PropWare
3.0.0.229
C++ objects and CMake build system for Parallax Propeller
propgcc
propeller-elf
include
sys
thread.h
1
/*
2
* Implementation of simple threading support
3
* Copyright (c) 2011 Parallax, Inc.
4
*
5
* Permission is hereby granted, free of charge, to any person obtaining
6
* a copy of this software and associated documentation files
7
* (the "Software"), to deal in the Software without restriction,
8
* including without limitation the rights to use, copy, modify, merge,
9
* publish, distribute, sublicense, and/or sell copies of the Software,
10
* and to permit persons to whom the Software is furnished to do so,
11
* subject to the following conditions:
12
*
13
* The above copyright notice and this permission notice shall be
14
* included in all copies or substantial portions of the Software.
15
*
16
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
* +--------------------------------------------------------------------
24
*/
25
#ifndef _SYS_THREAD_H
26
#define _SYS_THREAD_H
27
28
#include <sys/jmpbuf.h>
29
#include <sys/fenv.h>
30
#include <sys/wchar_t.h>
31
32
#if defined(__cplusplus)
33
extern
"C"
{
34
#endif
35
36
#ifndef _STRUCT_TM_DEFINED
37
#define _STRUCT_TM_DEFINED
38
/* time representing broken down calendar time */
39
struct
tm
{
40
int
tm_sec;
41
int
tm_min;
42
int
tm_hour;
43
int
tm_mday;
44
int
tm_mon;
45
int
tm_year;
/* years since 1900 */
46
int
tm_wday;
/* days since Sunday */
47
int
tm_yday;
/* days since January 1 */
48
int
tm_isdst;
/* if > 0, DST is in effect, if < 0 info is not known */
49
};
50
#endif
51
52
/*
53
* thread local storage
54
* the library should not keep anything in global or
55
* static variables; all such values should be placed
56
* in the _thread structure
57
*/
58
typedef
struct
_thread
_thread_state_t
;
59
typedef
_thread_state_t
*
volatile
_thread_queue_t;
60
61
struct
_thread
{
62
/* threads may sleep on queues */
63
_thread_state_t
*queue_next;
64
_thread_queue_t *queue;
65
66
int
errno;
67
char
*strtok_scanpoint;
68
69
unsigned
long
rand_seed;
70
struct
tm
time_temp;
71
char
ctime_buf[32];
72
_jmp_buf jmpbuf;
/* for thread context */
73
short
pri;
/* thread priority */
74
unsigned
short
flags;
/* flags for this thread */
75
76
unsigned
int
timer;
/* used for sleeping */
77
/* re-use arg for the thread return value */
78
void
*arg;
/* thread argument */
79
void
*(*start)(
void
*);
/* start function */
80
81
/* cog affinity mask: note that this is actually the inverse of
82
the affinity mask, so if it's 0 (the default) we can run on any
83
cog
84
*/
85
unsigned
short
affinity;
/* processor affinity mask inverted */
86
87
/* multibyte character conversion state */
88
_Mbstate_t
mbr_intern;
89
90
/* floating point environment; not used currently */
91
/* _fenv_t fenv; */
92
93
/* thread specific data for users to store */
94
#define _THREAD_KEYS_MAX 4
95
void
*thread_data[_THREAD_KEYS_MAX];
96
};
97
98
/*
99
* we may want to define thread local storage in some special way
100
* to ensure it is preserved in task switches
101
*/
102
#if defined(__propeller__) && defined(__GNUC__)
103
#define _TLSDECL(x) extern __attribute__((cogmem)) x
104
#else
105
#define _TLSDECL(x) extern x
106
#endif
107
108
_TLSDECL(
_thread_state_t
*_TLS );
109
110
/*
111
* start a new cog thread running C code
112
* (as if "func(arg)" was called in that thread)
113
* tls is a pointer to the thread local storage area to use
114
*/
115
int
_start_cog_thread(
void
*stacktop,
void
(*func)(
void
*),
void
*arg,
_thread_state_t
*tls);
116
117
/*
118
* function to do a "compare and swap" operation on a memory location
119
* this is protected with the _C_LOCK lock, so it should be atomic
120
* (as long as all threads use this function to access the location)
121
* if *ptr == checkval, then set *ptr to newval; returns original
122
* value of *ptr
123
*/
124
#if !defined(__GNUC__)
125
#define __sync_bool_compare_and_swap(ptr, oldval, newval) \
126
((*ptr == oldval) && ((*ptr = newval),true)))
127
#define __sync_add_and_fetch(ptr, inc) \
128
(*ptr += inc)
129
#endif
130
131
/* functions to atomicly fetch or set a 64 bit quantity in HUB memory */
132
/* by "atomic" we mean in 2 consecutive hub operations, low then high;
133
all users must follow this protocol
134
*/
135
unsigned
long
long
_getAtomic64(
unsigned
long
long
*ptr);
136
void
_putAtomic64(
unsigned
long
long
x
,
unsigned
long
long
*ptr);
137
138
/* type for a volatile lock */
139
/* if we change this type, change the definitions of SIG_ATOMIC_{MIN,MAX}
140
* in stdint.h too
141
*/
142
/* also note that in practice these have to go in HUB memory */
143
typedef
volatile
int
_atomic_t;
144
typedef
_atomic_t atomic_t;
145
146
#if !defined(__cplusplus)
147
/* the GNU C++ library already has locks in it, so don't conflict */
148
149
/* we don't have the necessary primitives in the XMM kernel */
150
#define __trylock(ptr) __sync_bool_compare_and_swap(ptr, 0, 1)
151
#define __addlock(ptr, inc) __sync_add_and_fetch(ptr, inc)
152
153
#if defined(__GNUC__)
154
#define __lock(val) while (__builtin_expect(!__trylock(val), 0)) ;
155
#else
156
#define __lock(val) while (!__trylock(val)) ;
157
#endif
158
#define __unlock(val) *val = 0
159
160
#endif
/* !defined(__cplusplus) */
161
162
/* hook for giving up CPU time while waiting */
163
extern
void (*__yield_ptr)(void);
164
165
/* hook for sleeping until the clock() reaches a specific count */
166
extern
void (*__napuntil_ptr)(
unsigned
int
newclock);
167
extern
void
__napuntil(
unsigned
int
newclock);
168
169
/* macro for finding our own CPU id in a bit mask */
170
#define __this_cpu_mask() (1<<__builtin_propeller_cogid())
171
172
#if defined(__cplusplus)
173
}
174
#endif
175
176
#endif
_Mbstate
Definition:
wchar_t.h:23
tm
Definition:
thread.h:39
x
int x
Definition:
07 Box and Lines.c:13
_thread
Definition:
thread.h:61
Generated on Fri May 20 2022 16:48:04 for PropWare by
1.8.17