forked from MDSplus/mdsplus
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix: split pthread into logical sections (MDSplus#1881)
* Fix: split pthread_port into logical sections * Fix: rhel5 requires _GNU_SOURCE
- Loading branch information
Showing
24 changed files
with
326 additions
and
371 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
#ifndef CONDITION_H | ||
#define CONDITION_H | ||
#include <pthread_port.h> | ||
|
||
typedef struct _Condition { | ||
pthread_cond_t cond; | ||
pthread_mutex_t mutex; | ||
int value; | ||
} Condition; | ||
|
||
typedef struct _Condition_p { | ||
pthread_cond_t cond; | ||
pthread_mutex_t mutex; | ||
void* value; | ||
} Condition_p; | ||
|
||
#define CONDITION_INITIALIZER {PTHREAD_COND_INITIALIZER,PTHREAD_MUTEX_INITIALIZER,B_FALSE} | ||
|
||
#define CONDITION_INIT(input) do{\ | ||
(input)->value = 0;\ | ||
pthread_cond_init(&(input)->cond, pthread_condattr_default);\ | ||
pthread_mutex_init(&(input)->mutex, pthread_mutexattr_default);\ | ||
} while(0) | ||
#define _CONDITION_LOCK(input) pthread_mutex_lock(&(input)->mutex) | ||
#define _CONDITION_UNLOCK(input) pthread_mutex_unlock(&(input)->mutex) | ||
#define _CONDITION_SIGNAL(input) pthread_cond_signal(&(input)->cond) | ||
#define _CONDITION_WAIT(input) pthread_cond_wait(&(input)->cond,&(input)->mutex) | ||
#define _CONDITION_WAIT_SET(input) while (!(input)->value) _CONDITION_WAIT(input) | ||
#define _CONDITION_WAIT_RESET(input) while ( (input)->value) _CONDITION_WAIT(input) | ||
#define _CONDITION_WAIT_1SEC(input,status) do{\ | ||
struct timespec tp;\ | ||
clock_gettime(CLOCK_REALTIME, &tp);\ | ||
tp.tv_sec++;\ | ||
status pthread_cond_timedwait(&(input)->cond,&(input)->mutex,&tp);\ | ||
} while(0) | ||
#define CONDITION_SET_TO(input,value_in) do{\ | ||
_CONDITION_LOCK(input);\ | ||
(input)->value = value_in;\ | ||
_CONDITION_SIGNAL(input);\ | ||
_CONDITION_UNLOCK(input);\ | ||
} while(0) | ||
#define CONDITION_SET(input) CONDITION_SET_TO(input,B_TRUE) | ||
#define CONDITION_RESET(input) CONDITION_SET_TO(input,0) | ||
#define CONDITION_WAIT_SET(input) do{\ | ||
_CONDITION_LOCK(input);\ | ||
_CONDITION_WAIT_SET(input);\ | ||
_CONDITION_UNLOCK(input);\ | ||
} while(0) | ||
#define CONDITION_WAIT_1SEC(input) do{\ | ||
_CONDITION_LOCK(input);\ | ||
_CONDITION_WAIT_1SEC(input,);\ | ||
_CONDITION_UNLOCK(input);\ | ||
} while(0) | ||
#define CONDITION_DESTROY(input,destroy_lock) do{\ | ||
pthread_mutex_lock(destroy_lock);\ | ||
pthread_cond_destroy(&(input)->cond);\ | ||
pthread_mutex_destroy(&(input)->mutex);\ | ||
pthread_mutex_unlock(destroy_lock);\ | ||
} while(0) | ||
#define CONDITION_DESTROY_PTR(input,destroy_lock) do{\ | ||
pthread_mutex_lock(destroy_lock);\ | ||
if (input){\ | ||
pthread_cond_destroy(&(input)->cond);\ | ||
pthread_mutex_destroy(&(input)->mutex);\ | ||
free(input);(input)=NULL;}\ | ||
pthread_mutex_unlock(destroy_lock);\ | ||
} while(0) | ||
#define CREATE_THREAD(thread, stacksize, target, args)\ | ||
pthread_attr_t attr;\ | ||
pthread_attr_init(&attr);\ | ||
pthread_attr_setstacksize(&attr, DEFAULT_STACKSIZE stacksize);\ | ||
int c_status = pthread_create(&thread, &attr, (void *)target, (void*)args);\ | ||
pthread_attr_destroy(&attr) | ||
#define CREATE_DETACHED_THREAD(thread, stacksize, target, args)\ | ||
CREATE_THREAD(thread, stacksize, target, args);if (!c_status) pthread_detach(thread); | ||
|
||
#define CONDITION_START_THREAD(input, thread, stacksize, target, args) do{\ | ||
_CONDITION_LOCK(input);\ | ||
if (!(input)->value) {\ | ||
CREATE_DETACHED_THREAD(thread, stacksize, target, args);\ | ||
if (c_status) {\ | ||
perror("Error creating pthread");\ | ||
status = MDSplusERROR;\ | ||
} else {\ | ||
_CONDITION_WAIT_SET(input);\ | ||
status = MDSplusSUCCESS;\ | ||
}\ | ||
}\ | ||
_CONDITION_UNLOCK(input);\ | ||
} while(0)//" | ||
|
||
#endif// CONDITION_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
#ifndef GETUSERNAME_H | ||
#define GETUSERNAME_H | ||
#ifdef HAVE_UNISTD_H | ||
#include <unistd.h> | ||
#endif | ||
#ifdef _WIN32 | ||
#include <io.h> | ||
#else | ||
#include <pwd.h> | ||
#endif | ||
#include <pthread_port.h> | ||
|
||
#define GETUSERNAME(user_p) GETUSERNAME_BEGIN(user_p);GETUSERNAME_END; | ||
|
||
#define GETUSERNAME_BEGIN(user_p) {\ | ||
static pthread_mutex_t username_mutex = PTHREAD_MUTEX_INITIALIZER;\ | ||
pthread_mutex_lock(&username_mutex);\ | ||
if (!user_p) {\ | ||
user_p = _getUserName() | ||
|
||
#define GETUSERNAME_END }\ | ||
pthread_mutex_unlock(&username_mutex);\ | ||
} | ||
static char* _getUserName(){ | ||
char *user_p; | ||
#ifdef _WIN32 | ||
static char user[128]; | ||
DWORD bsize = 128; | ||
user_p = GetUserName(user, &bsize) ? user : "Windows User"; | ||
#elif __MWERKS__ | ||
ans.pointer = "Macintosh User"; | ||
#else | ||
static char user[256]; | ||
struct passwd *pwd = getpwuid(geteuid()); | ||
if (pwd) { | ||
strcpy(user,pwd->pw_name); | ||
user_p = user; | ||
} else | ||
#ifdef __APPLE__ | ||
user_p = "Apple User"; | ||
#else | ||
{ | ||
user_p = getlogin(); | ||
if (user_p && strlen(user_p)>0){ | ||
strcpy(user,user_p); | ||
user_p = user; | ||
} else | ||
user_p = "Linux User"; | ||
} | ||
#endif | ||
#endif | ||
return user_p; | ||
} | ||
|
||
#endif// GETUSERNAME_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.