]> mj.ucw.cz Git - libucw.git/blob - ucw/threads.h
Opt: Streamlined opt_shortopt()
[libucw.git] / ucw / threads.h
1 /*
2  *      The UCW Library -- Threading Helpers
3  *
4  *      (c) 2006--2012 Martin Mares <mj@ucw.cz>
5  *
6  *      This software may be freely distributed and used according to the terms
7  *      of the GNU Lesser General Public License.
8  */
9
10 #ifndef _UCW_THREADS_H
11 #define _UCW_THREADS_H
12
13 #include <ucw/sighandler.h>
14
15 /* This structure holds per-thread data */
16
17 struct ucwlib_context {
18   int _thread_id;                       // Thread ID (either kernel tid or a counter, use ucwlib_thread_id())
19   int temp_counter;                     // Counter for fb-temp.c
20   struct asio_queue *io_queue;          // Async I/O queue for fb-direct.c
21   ucw_sighandler_t *signal_handlers;    // Signal handlers for sighandler.c
22   struct main_context *main_context;    // Current context for mainloop.c
23   struct cf_context *cf_context;        // Current context for configuration parser
24   // Resources and transactions:
25   struct respool *current_respool;      // Current resource pool
26   struct mempool *trans_pool;           // Transaction mempool
27   struct trans *current_trans;          // Currently open transaction
28 };
29
30 #ifdef CONFIG_UCW_THREADS
31
32 #ifdef CONFIG_UCW_TLS
33 extern __thread struct ucwlib_context ucwlib_context;
34 static inline struct ucwlib_context *ucwlib_thread_context(void) { return &ucwlib_context; }
35 int ucwlib_thread_id(struct ucwlib_context *c);
36 #else
37 struct ucwlib_context *ucwlib_thread_context(void);
38 static inline int ucwlib_thread_id(struct ucwlib_context *c) { return c->_thread_id; }
39 #endif
40
41 /* Global lock used for initialization, cleanup and other not so frequently accessed global state */
42
43 void ucwlib_lock(void);
44 void ucwlib_unlock(void);
45
46 extern uns ucwlib_thread_stack_size;
47
48 #else
49
50 /* We have no threads, let's simulate the context and locking */
51
52 extern struct ucwlib_context default_ucwlib_context;
53 static inline struct ucwlib_context *ucwlib_thread_context(void) { return &default_ucwlib_context; }
54
55 static inline int ucwlib_thread_id(struct ucwlib_context *c UNUSED) { return 0; }
56
57 static inline void ucwlib_lock(void) { }
58 static inline void ucwlib_unlock(void) { }
59
60 #endif
61
62 #endif