-
Notifications
You must be signed in to change notification settings - Fork 1
/
xpower-thread.h
114 lines (97 loc) · 3.46 KB
/
xpower-thread.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*
** Copyright (C) 2011 XXX all rights reserved.
**
** Copy thread and mutex controls from Objective C.
** Created by chuck-liu@qq.com on 01/11/2011
**
*/
#ifndef _XPOWER_THREAD_H_
#define _XPOWER_THREAD_H_
#ifdef __cplusplus
extern "C" {
#endif /** __cplusplus */
extern int _xthread_exit_status; /* global exit status */
/* thread priorities */
#define _XTHREAD_INTERACTIVE_PRIORITY 2
#define _XTHREAD_BACKGROUND_PRIORITY 1
#define _XTHREAD_LOW_PRIORITY 0
/* a thread */
typedef void *_xthread_t;
/* this structure represents a single mutual exclusion lock */
struct _xmutex {
volatile _xthread_t owner;/* id of thread that owns */
volatile int depth; /* # of acquires */
void *backend; /* specific to backend */
};
typedef struct _xmutex *_xmutex_t;
extern _xmutex_t _xthread_runtime_mutex;
/* this structure represents a single condition mutex */
struct _xcondition {
void *backend; /* specific to backend */
};
typedef struct _xcondition *_xcondition_t;
/* frontend mutex functions */
_xmutex_t _xmutex_allocate(void);
int _xmutex_deallocate(_xmutex_t mutex);
int _xmutex_lock(_xmutex_t mutex);
int _xmutex_unlock(_xmutex_t mutex);
int _xmutex_trylock(_xmutex_t mutex);
/* frontend condition mutex functions */
_xcondition_t _xcondition_allocate(void);
int _xcondition_deallocate(_xcondition_t condition);
int _xcondition_wait(_xcondition_t condition, _xmutex_t mutex);
int _xcondition_signal(_xcondition_t condition);
int _xcondition_broadcast(_xcondition_t condition);
/* frontend thread functions */
_xthread_t _xthread_id(void);
int _xthread_exit(void);
int _xthread_set_priority(int priority);
int _xthread_get_priority(void);
int _xthread_set_data(void *value);
void _xthread_add(void);
void _xthread_remove(void);
void _xthread_yield(void);
void *_xthread_get_data(void);
void _xthread_init(void);
/*!
* use this to set the hook function that will be called when the
* runtime initially becomes multi threaded.
* the hook function is only called once, meaning only when the 2nd
* thread is spawned, not for each and every thread.
*
* it returns the previous hook function or NULL if there is none.
* a program outside of the runtime could set this to some function
* so it can be informed; for example, the GNUstep Base Library sets
* it so it can implement the NSBecommingMultiThreaded notification.
*
*/
typedef void (*_xthread_callback)(void);
_xthread_callback _xthread_set_callback(_xthread_callback func);
/* backend initialization functions */
int __xinit_thread_system(void);
int __xclose_thread_system(void);
/* backend mutex functions */
int __xmutex_allocate(_xmutex_t mutex);
int __xmutex_deallocate(_xmutex_t mutex);
int __xmutex_lock(_xmutex_t mutex);
int __xmutex_trylock(_xmutex_t mutex);
int __xmutex_unlock(_xmutex_t mutex);
/* backend condition mutex functions */
int __xcondition_allocate(_xcondition_t condition);
int __xcondition_deallocate(_xcondition_t condition);
int __xcondition_wait(_xcondition_t condition, _xmutex_t mutex);
int __xcondition_broadcast(_xcondition_t condition);
int __xcondition_signal(_xcondition_t condition);
/* backend thread functions */
_xthread_t __xthread_detach(void (*func)(void *arg), void *arg);
_xthread_t __xthread_id(void);
int __xthread_set_priority(int priority);
int __xthread_get_priority(void);
int __xthread_exit(void);
int __xthread_set_data(void *value);
void __xthread_yield(void);
void *__xthread_get_data(void);
#ifdef __cplusplus
}
#endif /** __cplusplus */
#endif/*_XPOWER_THREAD_H_*/