source: trunk/libtransmission/platform.h @ 2197

Last change on this file since 2197 was 2197, checked in by charles, 15 years ago

building up our threading tools: implement RW locks

  • Property svn:keywords set to Date Rev Author Id
File size: 3.4 KB
Line 
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
38typedef struct tr_thread_s
39{
40    void          (* func ) ( void * );
41    void           * arg;
42    char           * name;
43    tr_thread_id_t thread;;
44}
45tr_thread_t;
46
47const char * tr_getHomeDirectory( void );
48const char * tr_getCacheDirectory( void );
49const 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 */ 
55const tr_thread_t THREAD_EMPTY;
56
57void tr_threadCreate ( tr_thread_t *, void (*func)(void *),
58                       void * arg, const char * name );
59void tr_threadJoin   ( tr_thread_t * );
60void tr_lockInit     ( tr_lock_t * );
61void tr_lockClose    ( tr_lock_t * );
62int  tr_lockTryLock  ( tr_lock_t * );
63void tr_lockLock     ( tr_lock_t * );
64void tr_lockUnlock   ( tr_lock_t * );
65
66void tr_condInit      ( tr_cond_t * );
67void tr_condSignal    ( tr_cond_t * );
68void tr_condBroadcast ( tr_cond_t * );
69void tr_condClose     ( tr_cond_t * );
70void 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
77typedef 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}
87tr_rwlock_t;
88
89void  tr_rwInit          ( tr_rwlock_t * );
90void  tr_rwClose         ( tr_rwlock_t * );
91void  tr_rwReaderLock    ( tr_rwlock_t * );
92int   tr_rwReaderTrylock ( tr_rwlock_t * );
93void  tr_rwReaderUnlock  ( tr_rwlock_t * );
94void  tr_rwWriterLock    ( tr_rwlock_t * );
95int   tr_rwWriterTrylock ( tr_rwlock_t * );
96void  tr_rwWriterUnlock  ( tr_rwlock_t * );
97
98
99struct in_addr; /* forward declaration to calm gcc down */
100int
101tr_getDefaultRoute( struct in_addr * addr );
102
103#endif
Note: See TracBrowser for help on using the repository browser.