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 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468
|
////////////////////////////////////////////////////////////////////////////////////
// fthreads.h Fish's WIN32 version of pthreads
////////////////////////////////////////////////////////////////////////////////////
// (c) Copyright "Fish" (David B. Trout), 2001-2009. Released under the Q Public License
// (http://www.hercules-390.org/herclic.html) as modifications to Hercules.
////////////////////////////////////////////////////////////////////////////////////
// $Id: fthreads.h 5125 2009-01-23 12:01:44Z bernard $
//
// $Log$
// Revision 1.24 2008/03/25 11:41:31 fish
// SCSI TAPE MODS part 1: groundwork: non-functional changes:
// rename some functions, comments, general restructuring, etc.
// New source modules awstape.c, omatape.c, hettape.c and
// tapeccws.c added, but not yet used (all will be used in a future
// commit though when tapedev.c code is eventually split)
//
// Revision 1.23 2007/11/30 14:54:32 jmaynard
// Changed conmicro.cx to hercules-390.org or conmicro.com, as needed.
//
// Revision 1.22 2007/06/23 00:04:10 ivan
// Update copyright notices to include current year (2007)
//
// Revision 1.21 2007/01/10 09:29:45 fish
// Fix thread naming that was inadvertently broken by my 12/28 fishhang change that introduced use of _beginthreadex
//
// Revision 1.20 2006/12/08 09:43:21 jj
// Add CVS message log
//
#ifndef _FTHREADS_H_
#define _FTHREADS_H_
#include "hercules.h"
#include "fishhang.h"
#ifndef _FTHREADS_C_
#ifndef _HUTIL_DLL_
#define FT_DLL_IMPORT DLL_IMPORT
#else /* _HUTIL_DLL_ */
#define FT_DLL_IMPORT extern
#endif /* _HUTIL_DLL_ */
#else
#define FT_DLL_IMPORT DLL_EXPORT
#endif
////////////////////////////////////////////////////////////////////////////////////
// Just a handy macro to have around...
#define RC(rc) (errno = rc)
////////////////////////////////////////////////////////////////////////////////////
// (need struct timespec for fthread_cond_timedwait)
#if !defined(TIMESPEC_IN_SYS_TYPES_H) && !defined(TIMESPEC_IN_TIME_H)
// (need to define it ourselves)
struct timespec
{
time_t tv_sec; // (seconds)
long tv_nsec; // (nanoseconds)
};
#endif
////////////////////////////////////////////////////////////////////////////////////
// fthread typedefs...
typedef void* FT_W32_HANDLE; // HANDLE
typedef unsigned long FT_W32_DWORD; // DWORD
typedef FT_W32_DWORD fthread_t; // thread id
typedef FT_W32_DWORD fthread_mutexattr_t; // mutex attribute
typedef void* (FT_THREAD_FUNC)(void*); // thread function
typedef FT_THREAD_FUNC* PFT_THREAD_FUNC; // thread function ptr
typedef struct _tagFTU_MUTEX // fthread "mutex" structure
{
FT_W32_DWORD dwMutexMagic; // (magic number)
FT_W32_HANDLE hMutex; // (ptr to actual mutex structure)
}
fthread_mutex_t;
typedef struct _tagFTU_COND // fthread "condition variable" structure
{
FT_W32_DWORD dwCondMagic; // (magic number)
FT_W32_HANDLE hCondVar; // (ptr to actual condition variable structure)
}
fthread_cond_t;
typedef struct _tagFTU_ATTR // fthread "thread attribute" structure
{
FT_W32_DWORD dwAttrMagic; // (magic number)
size_t nStackSize; // (initial stack size in bytes)
int nDetachState; // (requested detach state: detached/joinable)
}
fthread_attr_t;
////////////////////////////////////////////////////////////////////////////////////
// fthread thread attribute types...
#define FTHREAD_CREATE_JOINABLE 0x4A6F696E // "Join" in ASCII
#define FTHREAD_CREATE_DETACHED 0x44697363 // "Disc" in ASCII
#define FTHREAD_CREATE_DEFAULT FTHREAD_CREATE_JOINABLE
////////////////////////////////////////////////////////////////////////////////////
// Initialize a "thread attribute"...
FT_DLL_IMPORT
int fthread_attr_init
(
fthread_attr_t* pThreadAttr
);
////////////////////////////////////////////////////////////////////////////////////
// Destroy a "thread attribute"...
FT_DLL_IMPORT
int fthread_attr_destroy
(
fthread_attr_t* pThreadAttr
);
////////////////////////////////////////////////////////////////////////////////////
// Set a thread's "detachstate" attribute...
FT_DLL_IMPORT
int fthread_attr_setdetachstate
(
fthread_attr_t* pThreadAttr,
int nDetachState
);
////////////////////////////////////////////////////////////////////////////////////
// Retrieve a thread's "detachstate" attribute...
FT_DLL_IMPORT
int fthread_attr_getdetachstate
(
const fthread_attr_t* pThreadAttr,
int* pnDetachState
);
////////////////////////////////////////////////////////////////////////////////////
// Set a thread's initial stack size...
FT_DLL_IMPORT
int fthread_attr_setstacksize
(
fthread_attr_t* pThreadAttr,
size_t nStackSize
);
////////////////////////////////////////////////////////////////////////////////////
// Retrieve a thread's initial stack size...
FT_DLL_IMPORT
int fthread_attr_getstacksize
(
const fthread_attr_t* pThreadAttr,
size_t* pnStackSize
);
////////////////////////////////////////////////////////////////////////////////////
// Join a thread (i.e. wait for a thread's termination)...
FT_DLL_IMPORT
int fthread_join
(
#ifdef FISH_HANG
const char* pszFile,
const int nLine,
#endif
fthread_t dwThreadID,
void** pExitVal
);
////////////////////////////////////////////////////////////////////////////////////
// Detach a thread (i.e. ignore a thread's termination)...
FT_DLL_IMPORT
int fthread_detach
(
fthread_t dwThreadID
);
////////////////////////////////////////////////////////////////////////////////////
// Create a new thread...
FT_DLL_IMPORT
int fthread_create
(
#ifdef FISH_HANG
const char* pszFile,
const int nLine,
#endif
fthread_t* pdwThreadID,
fthread_attr_t* pThreadAttr,
PFT_THREAD_FUNC pfnThreadFunc,
void* pvThreadArgs,
char* pszThreadName
);
////////////////////////////////////////////////////////////////////////////////////
// Exit from a thread...
FT_DLL_IMPORT
void fthread_exit
(
void* ExitVal
);
////////////////////////////////////////////////////////////////////////////////////
// Return thread-id...
FT_DLL_IMPORT
fthread_t fthread_self
(
);
////////////////////////////////////////////////////////////////////////////////////
// Compare thread-ids...
FT_DLL_IMPORT
int fthread_equal
(
fthread_t pdwThreadID_1,
fthread_t pdwThreadID_2
);
////////////////////////////////////////////////////////////////////////////////////
// (thread signalling not [currently] supported (yet); always returns ENOTSUP...)
FT_DLL_IMPORT
int fthread_kill // FIXME: TODO:
(
int dummy1,
int dummy2
);
////////////////////////////////////////////////////////////////////////////////////
// Initialize a "mutex"...
FT_DLL_IMPORT
int fthread_mutex_init
(
#ifdef FISH_HANG
const char* pszFile,
const int nLine,
#endif
fthread_mutex_t* pFT_MUTEX,
const fthread_mutexattr_t* pFT_MUTEX_ATTR
);
////////////////////////////////////////////////////////////////////////////////////
// Destroy a "mutex"...
FT_DLL_IMPORT
int fthread_mutex_destroy
(
#ifdef FISH_HANG
const char* pszFile,
const int nLine,
#endif
fthread_mutex_t* pFT_MUTEX
);
////////////////////////////////////////////////////////////////////////////////////
// Lock a "mutex"...
FT_DLL_IMPORT
int fthread_mutex_lock
(
#ifdef FISH_HANG
const char* pszFile,
const int nLine,
#endif
fthread_mutex_t* pFT_MUTEX
);
////////////////////////////////////////////////////////////////////////////////////
// Try to lock a "mutex"...
FT_DLL_IMPORT
int fthread_mutex_trylock
(
#ifdef FISH_HANG
const char* pszFile,
const int nLine,
#endif
fthread_mutex_t* pFT_MUTEX
);
////////////////////////////////////////////////////////////////////////////////////
// Unlock a "mutex"...
FT_DLL_IMPORT
int fthread_mutex_unlock
(
#ifdef FISH_HANG
const char* pszFile,
const int nLine,
#endif
fthread_mutex_t* pFT_MUTEX
);
////////////////////////////////////////////////////////////////////////////////////
// Initialize a "condition"...
FT_DLL_IMPORT
int fthread_cond_init
(
#ifdef FISH_HANG
const char* pszFile,
const int nLine,
#endif
fthread_cond_t* pFT_COND_VAR
);
////////////////////////////////////////////////////////////////////////////////////
// Destroy a "condition"...
FT_DLL_IMPORT
int fthread_cond_destroy
(
#ifdef FISH_HANG
const char* pszFile,
const int nLine,
#endif
fthread_cond_t* pFT_COND_VAR
);
////////////////////////////////////////////////////////////////////////////////////
// 'Signal' a "condition"... (causes ONE waiting thread to be released)
FT_DLL_IMPORT
int fthread_cond_signal
(
#ifdef FISH_HANG
const char* pszFile,
const int nLine,
#endif
fthread_cond_t* pFT_COND_VAR
);
////////////////////////////////////////////////////////////////////////////////////
// 'Broadcast' a "condition"... (causes ALL waiting threads to be released)
FT_DLL_IMPORT
int fthread_cond_broadcast
(
#ifdef FISH_HANG
const char* pszFile,
const int nLine,
#endif
fthread_cond_t* pFT_COND_VAR
);
////////////////////////////////////////////////////////////////////////////////////
// Wait for a "condition" to occur...
FT_DLL_IMPORT
int fthread_cond_wait
(
#ifdef FISH_HANG
const char* pszFile,
const int nLine,
#endif
fthread_cond_t* pFT_COND_VAR,
fthread_mutex_t* pFT_MUTEX
);
////////////////////////////////////////////////////////////////////////////////////
// Wait (but not forever) for a "condition" to occur...
FT_DLL_IMPORT
int fthread_cond_timedwait
(
#ifdef FISH_HANG
const char* pszFile,
const int nLine,
#endif
fthread_cond_t* pFT_COND_VAR,
fthread_mutex_t* pFT_MUTEX,
struct timespec* pTimeTimeout
);
////////////////////////////////////////////////////////////////////////////////////
// fthread mutex attribute types...
//
// FTHREAD_MUTEX_NORMAL This type of mutex does not detect deadlock. A thread
// attempting to relock this mutex without first unlocking
// it shall deadlock. Attempting to unlock a mutex locked
// by a different thread results in undefined behavior.
// Attempting to unlock an unlocked mutex results in
// undefined behavior. The FTHREAD_MUTEX_NORMAL mutex type
// is not currently supported by fthreads.
//
// FTHREAD_MUTEX_ERRORCHECK This type of mutex provides error checking. A thread
// attempting to relock this mutex without first unlocking
// it shall return with an error. A thread attempting to
// unlock a mutex which another thread has locked shall
// return with an error. A thread attempting to unlock an
// unlocked mutex shall return with an error.
//
// FTHREAD_MUTEX_RECURSIVE A thread attempting to relock this mutex without first
// unlocking it shall succeed in locking the mutex. The
// relocking deadlock which can occur with mutexes of type
// FTHREAD_MUTEX_NORMAL cannot occur with this type of mutex.
// Multiple locks of this mutex shall require the same number
// of unlocks to release the mutex before another thread
// can acquire the mutex. A thread attempting to unlock a
// mutex which another thread has locked shall return with
// an error. A thread attempting to unlock an unlocked mutex
// shall return with an error.
//
// FTHREAD_MUTEX_DEFAULT Attempting to recursively lock a mutex of this type results
// in undefined behavior. Attempting to unlock a mutex of this
// type which was not locked by the calling thread results
// in undefined behavior. Attempting to unlock a mutex of this
// type which is not locked results in undefined behavior.
// An implementation may map this mutex to one of the other
// mutex types.
#define FTHREAD_MUTEX_ERRORCHECK 0x4F6E6365 // "Once" in ASCII
#define FTHREAD_MUTEX_RECURSIVE 0x4D616E79 // "Many" in ASCII
#define FTHREAD_MUTEX_DEFAULT FTHREAD_MUTEX_ERRORCHECK
////////////////////////////////////////////////////////////////////////////////////
// Initialize a "mutex" attribute...
FT_DLL_IMPORT
int fthread_mutexattr_init
(
fthread_mutexattr_t* pFT_MUTEX_ATTR
);
////////////////////////////////////////////////////////////////////////////////////
// Destroy a "mutex" attribute...
FT_DLL_IMPORT
int fthread_mutexattr_destroy
(
fthread_mutexattr_t* pFT_MUTEX_ATTR
);
////////////////////////////////////////////////////////////////////////////////////
// Retrieve "mutex" attribute type...
FT_DLL_IMPORT
int fthread_mutexattr_gettype
(
const fthread_mutexattr_t* pFT_MUTEX_ATTR,
int* pnMutexType
);
////////////////////////////////////////////////////////////////////////////////////
// Set "mutex" attribute type...
FT_DLL_IMPORT
int fthread_mutexattr_settype
(
fthread_mutexattr_t* pFT_MUTEX_ATTR,
int nMutexType
);
////////////////////////////////////////////////////////////////////////////////////
#endif // _FTHREADS_H_
|