1 | /****************************************************************************** |
---|
2 | * $Id: platform.h 2197 2007-06-25 21:52:18Z charles $ |
---|
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 | typedef thread_id tr_thread_id_t; |
---|
30 | typedef sem_id tr_lock_t; |
---|
31 | typedef int tr_cond_t; |
---|
32 | #else |
---|
33 | #include <pthread.h> |
---|
34 | typedef pthread_t tr_thread_id_t; |
---|
35 | typedef pthread_mutex_t tr_lock_t; |
---|
36 | typedef pthread_cond_t tr_cond_t; |
---|
37 | #endif |
---|
38 | typedef struct tr_thread_s |
---|
39 | { |
---|
40 | void (* func ) ( void * ); |
---|
41 | void * arg; |
---|
42 | char * name; |
---|
43 | tr_thread_id_t thread;; |
---|
44 | } |
---|
45 | tr_thread_t; |
---|
46 | |
---|
47 | const char * tr_getHomeDirectory( void ); |
---|
48 | const char * tr_getCacheDirectory( void ); |
---|
49 | const char * tr_getTorrentsDirectory( void ); |
---|
50 | |
---|
51 | /** |
---|
52 | * When instantiating a thread with a deferred call to tr_threadCreate(), |
---|
53 | * initializing it to THREAD_EMPTY makes calls tr_threadJoin() safe. |
---|
54 | */ |
---|
55 | const tr_thread_t THREAD_EMPTY; |
---|
56 | |
---|
57 | void tr_threadCreate ( tr_thread_t *, void (*func)(void *), |
---|
58 | void * arg, const char * name ); |
---|
59 | void tr_threadJoin ( tr_thread_t * ); |
---|
60 | void tr_lockInit ( tr_lock_t * ); |
---|
61 | void tr_lockClose ( tr_lock_t * ); |
---|
62 | int tr_lockTryLock ( tr_lock_t * ); |
---|
63 | void tr_lockLock ( tr_lock_t * ); |
---|
64 | void tr_lockUnlock ( tr_lock_t * ); |
---|
65 | |
---|
66 | void tr_condInit ( tr_cond_t * ); |
---|
67 | void tr_condSignal ( tr_cond_t * ); |
---|
68 | void tr_condBroadcast ( tr_cond_t * ); |
---|
69 | void tr_condClose ( tr_cond_t * ); |
---|
70 | void tr_condWait ( tr_cond_t *, tr_lock_t * ); |
---|
71 | |
---|
72 | /*** |
---|
73 | **** RW lock: |
---|
74 | **** The lock can be had by one writer or any number of readers. |
---|
75 | ***/ |
---|
76 | |
---|
77 | typedef struct tr_rwlock_s |
---|
78 | { |
---|
79 | tr_lock_t lock; |
---|
80 | tr_cond_t readCond; |
---|
81 | tr_cond_t writeCond; |
---|
82 | size_t readCount; |
---|
83 | size_t wantToRead; |
---|
84 | size_t wantToWrite; |
---|
85 | int haveWriter; |
---|
86 | } |
---|
87 | tr_rwlock_t; |
---|
88 | |
---|
89 | void tr_rwInit ( tr_rwlock_t * ); |
---|
90 | void tr_rwClose ( tr_rwlock_t * ); |
---|
91 | void tr_rwReaderLock ( tr_rwlock_t * ); |
---|
92 | int tr_rwReaderTrylock ( tr_rwlock_t * ); |
---|
93 | void tr_rwReaderUnlock ( tr_rwlock_t * ); |
---|
94 | void tr_rwWriterLock ( tr_rwlock_t * ); |
---|
95 | int tr_rwWriterTrylock ( tr_rwlock_t * ); |
---|
96 | void tr_rwWriterUnlock ( tr_rwlock_t * ); |
---|
97 | |
---|
98 | |
---|
99 | struct in_addr; /* forward declaration to calm gcc down */ |
---|
100 | int |
---|
101 | tr_getDefaultRoute( struct in_addr * addr ); |
---|
102 | |
---|
103 | #endif |
---|