1 | /****************************************************************************** |
---|
2 | * $Id: platform.h 2323 2007-07-10 14:00:20Z titer $ |
---|
3 | * |
---|
4 | * Copyright (c) 2005 Transmission authors and contributors |
---|
5 | * |
---|
6 | * Permission is hereby granted, free of charge, to any person obtaining a |
---|
7 | * copy of this software and associated documentation files (the "Software"), |
---|
8 | * to deal in the Software without restriction, including without limitation |
---|
9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
---|
10 | * and/or sell copies of the Software, and to permit persons to whom the |
---|
11 | * Software is furnished to do so, subject to the following conditions: |
---|
12 | * |
---|
13 | * The above copyright notice and this permission notice shall be included in |
---|
14 | * all copies or substantial portions of the Software. |
---|
15 | * |
---|
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
---|
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
---|
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
---|
21 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
22 | * DEALINGS IN THE SOFTWARE. |
---|
23 | *****************************************************************************/ |
---|
24 | #ifndef TR_PLATFORM_H |
---|
25 | #define TR_PLATFORM_H 1 |
---|
26 | |
---|
27 | #ifdef SYS_BEOS |
---|
28 | #include <kernel/OS.h> |
---|
29 | #define BEOS_MAX_THREADS 256 |
---|
30 | typedef thread_id tr_thread_id_t; |
---|
31 | typedef sem_id tr_lock_t; |
---|
32 | typedef struct |
---|
33 | { |
---|
34 | sem_id sem; |
---|
35 | thread_id threads[BEOS_MAX_THREADS]; |
---|
36 | int start; |
---|
37 | int end; |
---|
38 | } tr_cond_t; |
---|
39 | #else |
---|
40 | #include <pthread.h> |
---|
41 | typedef pthread_t tr_thread_id_t; |
---|
42 | typedef pthread_mutex_t tr_lock_t; |
---|
43 | typedef pthread_cond_t tr_cond_t; |
---|
44 | #endif |
---|
45 | typedef struct tr_thread_s |
---|
46 | { |
---|
47 | void (* func ) ( void * ); |
---|
48 | void * arg; |
---|
49 | char * name; |
---|
50 | tr_thread_id_t thread; |
---|
51 | } |
---|
52 | tr_thread_t; |
---|
53 | |
---|
54 | const char * tr_getHomeDirectory( void ); |
---|
55 | const char * tr_getCacheDirectory( void ); |
---|
56 | const char * tr_getTorrentsDirectory( void ); |
---|
57 | |
---|
58 | /** |
---|
59 | * When instantiating a thread with a deferred call to tr_threadCreate(), |
---|
60 | * initializing it to THREAD_EMPTY makes calls tr_threadJoin() safe. |
---|
61 | */ |
---|
62 | const tr_thread_t THREAD_EMPTY; |
---|
63 | |
---|
64 | void tr_threadCreate ( tr_thread_t *, void (*func)(void *), |
---|
65 | void * arg, const char * name ); |
---|
66 | void tr_threadJoin ( tr_thread_t * ); |
---|
67 | void tr_lockInit ( tr_lock_t * ); |
---|
68 | void tr_lockClose ( tr_lock_t * ); |
---|
69 | int tr_lockTryLock ( tr_lock_t * ); |
---|
70 | void tr_lockLock ( tr_lock_t * ); |
---|
71 | void tr_lockUnlock ( tr_lock_t * ); |
---|
72 | |
---|
73 | void tr_condInit ( tr_cond_t * ); |
---|
74 | void tr_condSignal ( tr_cond_t * ); |
---|
75 | void tr_condBroadcast ( tr_cond_t * ); |
---|
76 | void tr_condClose ( tr_cond_t * ); |
---|
77 | void tr_condWait ( tr_cond_t *, tr_lock_t * ); |
---|
78 | |
---|
79 | /*** |
---|
80 | **** RW lock: |
---|
81 | **** The lock can be had by one writer or any number of readers. |
---|
82 | ***/ |
---|
83 | |
---|
84 | typedef struct tr_rwlock_s |
---|
85 | { |
---|
86 | tr_lock_t lock; |
---|
87 | tr_cond_t readCond; |
---|
88 | tr_cond_t writeCond; |
---|
89 | size_t readCount; |
---|
90 | size_t wantToRead; |
---|
91 | size_t wantToWrite; |
---|
92 | int haveWriter; |
---|
93 | } |
---|
94 | tr_rwlock_t; |
---|
95 | |
---|
96 | void tr_rwInit ( tr_rwlock_t * ); |
---|
97 | void tr_rwClose ( tr_rwlock_t * ); |
---|
98 | void tr_rwReaderLock ( tr_rwlock_t * ); |
---|
99 | int tr_rwReaderTrylock ( tr_rwlock_t * ); |
---|
100 | void tr_rwReaderUnlock ( tr_rwlock_t * ); |
---|
101 | void tr_rwWriterLock ( tr_rwlock_t * ); |
---|
102 | int tr_rwWriterTrylock ( tr_rwlock_t * ); |
---|
103 | void tr_rwWriterUnlock ( tr_rwlock_t * ); |
---|
104 | |
---|
105 | |
---|
106 | struct in_addr; /* forward declaration to calm gcc down */ |
---|
107 | int |
---|
108 | tr_getDefaultRoute( struct in_addr * addr ); |
---|
109 | |
---|
110 | #endif |
---|