Skip to content

Commit

Permalink
Fix: split pthread into logical sections (MDSplus#1881)
Browse files Browse the repository at this point in the history
* Fix: split pthread_port into logical sections

* Fix: rhel5 requires _GNU_SOURCE
  • Loading branch information
zack-vii authored Dec 18, 2019
1 parent d39f4dd commit 48e37c8
Show file tree
Hide file tree
Showing 24 changed files with 326 additions and 371 deletions.
3 changes: 3 additions & 0 deletions deploy/packaging/debian/devel.noarch
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,12 @@
./usr/local/mdsplus/include/camshr.h
./usr/local/mdsplus/include/camshr_messages.h
./usr/local/mdsplus/include/classdef.h
./usr/local/mdsplus/include/condition.h
./usr/local/mdsplus/include/coz.h
./usr/local/mdsplus/include/dbidef.h
./usr/local/mdsplus/include/dcl.h
./usr/local/mdsplus/include/dtypedef.h
./usr/local/mdsplus/include/getusername.h
./usr/local/mdsplus/include/int128.h
./usr/local/mdsplus/include/ipdesc.h
./usr/local/mdsplus/include/libroutines.h
Expand Down Expand Up @@ -66,6 +68,7 @@
./usr/local/mdsplus/include/rtevents.h
./usr/local/mdsplus/include/servershr.h
./usr/local/mdsplus/include/servershr_messages.h
./usr/local/mdsplus/include/socket_port.h
./usr/local/mdsplus/include/sqldb.h
./usr/local/mdsplus/include/sqlfront.h
./usr/local/mdsplus/include/status.h
Expand Down
3 changes: 3 additions & 0 deletions deploy/packaging/redhat/devel.noarch
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,12 @@
./usr/local/mdsplus/include/camshr.h
./usr/local/mdsplus/include/camshr_messages.h
./usr/local/mdsplus/include/classdef.h
./usr/local/mdsplus/include/condition.h
./usr/local/mdsplus/include/coz.h
./usr/local/mdsplus/include/dbidef.h
./usr/local/mdsplus/include/dcl.h
./usr/local/mdsplus/include/dtypedef.h
./usr/local/mdsplus/include/getusername.h
./usr/local/mdsplus/include/int128.h
./usr/local/mdsplus/include/ipdesc.h
./usr/local/mdsplus/include/libroutines.h
Expand Down Expand Up @@ -69,6 +71,7 @@
./usr/local/mdsplus/include/rtevents.h
./usr/local/mdsplus/include/servershr.h
./usr/local/mdsplus/include/servershr_messages.h
./usr/local/mdsplus/include/socket_port.h
./usr/local/mdsplus/include/sqldb.h
./usr/local/mdsplus/include/sqlfront.h
./usr/local/mdsplus/include/status.h
Expand Down
92 changes: 92 additions & 0 deletions include/condition.h
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
55 changes: 55 additions & 0 deletions include/getusername.h
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
164 changes: 4 additions & 160 deletions include/pthread_port.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@
#include <STATICdef.h>
#ifdef _WIN32
#ifndef NO_WINDOWS_H
#ifdef LOAD_INITIALIZESOCKETS
#include <winsock2.h>
#endif
#include <windows.h>
#endif
#endif
Expand Down Expand Up @@ -62,18 +59,6 @@ int rv = gettimeofday(&now, NULL);\
}
#endif

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;

// FREE
static void __attribute__((unused)) free_if(void *ptr){
free(*(void**)ptr);
Expand All @@ -97,149 +82,8 @@ static void __attribute__((unused)) fclose_if(void *ptr){
#define INIT_AS_AND_FCLOSE_ON_EXIT(ptr,value) FILE *ptr = value;FCLOSE_ON_EXIT(ptr)
#define INIT_AND_FCLOSE_ON_EXIT(ptr) INIT_AS_AND_FCLOSE_ON_EXIT(ptr,NULL)

#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)//"
#ifdef LOAD_INITIALIZESOCKETS
#ifndef _WIN32
#define INITIALIZESOCKETS
#else
#define SHUT_RDWR 2
static pthread_once_t InitializeSockets_once = PTHREAD_ONCE_INIT;
static void InitializeSockets() {
WSADATA wsaData;
WORD wVersionRequested;
wVersionRequested = MAKEWORD(1, 1);
WSAStartup(wVersionRequested, &wsaData);
}
#define INITIALIZESOCKETS pthread_once(&InitializeSockets_once,InitializeSockets)
#endif
#endif

#ifdef LOAD_GETUSERNAME
#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;
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

#define RUN_FUNCTION_ONCE(fun) do{ \
static pthread_once_t RUN_FUNCTION_once = PTHREAD_ONCE_INIT; \
static pthread_mutex_t RUN_FUNCTION_lock = PTHREAD_MUTEX_INITIALIZER; \
pthread_mutex_lock(&RUN_FUNCTION_lock); \
pthread_cleanup_push((void*)pthread_mutex_unlock,&RUN_FUNCTION_lock); \
pthread_once(&RUN_FUNCTION_once,fun); \
pthread_cleanup_pop(1); \
}while(0)

#define INIT_SHARED_FUNCTION_ONCE(fun) static pthread_once_t __##fun##__once = PTHREAD_ONCE_INIT
#define RUN_SHARED_FUNCTION_ONCE(fun) pthread_once(& __##fun##__once,(void*)fun)
#define RUN_FUNCTION_ONCE(fun) do{INIT_SHARED_FUNCTION_ONCE(fun);RUN_SHARED_FUNCTION_ONCE(fun);}while(0)

#endif//nPTHREAD_PORT_H
#endif// PTHREAD_PORT_H
Loading

0 comments on commit 48e37c8

Please sign in to comment.