Skip to content

Commit

Permalink
vim folds and mode lines yet
Browse files Browse the repository at this point in the history
  • Loading branch information
weltling committed Jul 4, 2017
1 parent a0203ea commit 6a3a2bf
Show file tree
Hide file tree
Showing 7 changed files with 153 additions and 88 deletions.
105 changes: 57 additions & 48 deletions TSRM/TSRM.c
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ TSRM_TLS uint8_t in_main_thread = 0;

/* Startup TSRM (call once for the entire process) */
TSRM_API int tsrm_startup(int expected_threads, int expected_resources, int debug_level, char *debug_filename)
{
{/*{{{*/
#if defined(GNUPTH)
pth_init();
#elif defined(PTHREADS)
Expand Down Expand Up @@ -166,12 +166,12 @@ TSRM_API int tsrm_startup(int expected_threads, int expected_resources, int debu

TSRM_ERROR((TSRM_ERROR_LEVEL_CORE, "Started up TSRM, %d expected threads, %d expected resources", expected_threads, expected_resources));
return 1;
}
}/*}}}*/


/* Shutdown TSRM (call once for the entire process) */
TSRM_API void tsrm_shutdown(void)
{
{/*{{{*/
int i;

if (!in_main_thread) {
Expand Down Expand Up @@ -227,12 +227,12 @@ TSRM_API void tsrm_shutdown(void)
tsrm_new_thread_begin_handler = NULL;
tsrm_new_thread_end_handler = NULL;
tsrm_shutdown_handler = NULL;
}
}/*}}}*/


/* allocates a new thread-safe-resource id */
TSRM_API ts_rsrc_id ts_allocate_id(ts_rsrc_id *rsrc_id, size_t size, ts_allocate_ctor ctor, ts_allocate_dtor dtor)
{
{/*{{{*/
int i;

TSRM_ERROR((TSRM_ERROR_LEVEL_CORE, "Obtaining a new resource id, %d bytes", size));
Expand Down Expand Up @@ -283,11 +283,11 @@ TSRM_API ts_rsrc_id ts_allocate_id(ts_rsrc_id *rsrc_id, size_t size, ts_allocate

TSRM_ERROR((TSRM_ERROR_LEVEL_CORE, "Successfully allocated new resource id %d", *rsrc_id));
return *rsrc_id;
}
}/*}}}*/


static void allocate_new_resource(tsrm_tls_entry **thread_resources_ptr, THREAD_T thread_id)
{
{/*{{{*/
int i;

TSRM_ERROR((TSRM_ERROR_LEVEL_CORE, "Creating data structures for thread %x", thread_id));
Expand Down Expand Up @@ -323,12 +323,12 @@ static void allocate_new_resource(tsrm_tls_entry **thread_resources_ptr, THREAD_
}

tsrm_mutex_unlock(tsmm_mutex);
}
}/*}}}*/


/* fetches the requested resource for the current thread */
TSRM_API void *ts_resource_ex(ts_rsrc_id id, THREAD_T *th_id)
{
{/*{{{*/
THREAD_T thread_id;
int hash_value;
tsrm_tls_entry *thread_resources;
Expand Down Expand Up @@ -386,12 +386,12 @@ TSRM_API void *ts_resource_ex(ts_rsrc_id id, THREAD_T *th_id)
* changes to the structure as we read it.
*/
TSRM_SAFE_RETURN_RSRC(thread_resources->storage, id, thread_resources->count);
}
}/*}}}*/

/* frees an interpreter context. You are responsible for making sure that
* it is not linked into the TSRM hash, and not marked as the current interpreter */
void tsrm_free_interpreter_context(void *context)
{
{/*{{{*/
tsrm_tls_entry *next, *thread_resources = (tsrm_tls_entry*)context;
int i;

Expand All @@ -410,10 +410,10 @@ void tsrm_free_interpreter_context(void *context)
free(thread_resources);
thread_resources = next;
}
}
}/*}}}*/

void *tsrm_set_interpreter_context(void *new_ctx)
{
{/*{{{*/
tsrm_tls_entry *current;

current = tsrm_tls_get();
Expand All @@ -426,12 +426,12 @@ void *tsrm_set_interpreter_context(void *new_ctx)

/* return old context, so caller can restore it when they're done */
return current;
}
}/*}}}*/


/* allocates a new interpreter context */
void *tsrm_new_interpreter_context(void)
{
{/*{{{*/
tsrm_tls_entry *new_ctx, *current;
THREAD_T thread_id;

Expand All @@ -445,12 +445,12 @@ void *tsrm_new_interpreter_context(void)
/* switch back to the context that was in use prior to our creation
* of the new one */
return tsrm_set_interpreter_context(current);
}
}/*}}}*/


/* frees all resources allocated for the current thread */
void ts_free_thread(void)
{
{/*{{{*/
tsrm_tls_entry *thread_resources;
int i;
THREAD_T thread_id = tsrm_thread_id();
Expand Down Expand Up @@ -487,12 +487,12 @@ void ts_free_thread(void)
thread_resources = thread_resources->next;
}
tsrm_mutex_unlock(tsmm_mutex);
}
}/*}}}*/


/* frees all resources allocated for all threads except current */
void ts_free_worker_threads(void)
{
{/*{{{*/
tsrm_tls_entry *thread_resources;
int i;
THREAD_T thread_id = tsrm_thread_id();
Expand Down Expand Up @@ -533,12 +533,12 @@ void ts_free_worker_threads(void)
}
}
tsrm_mutex_unlock(tsmm_mutex);
}
}/*}}}*/


/* deallocates all occurrences of a given id */
void ts_free_id(ts_rsrc_id id)
{
{/*{{{*/
int i;
int j = TSRM_UNSHUFFLE_RSRC_ID(id);

Expand Down Expand Up @@ -567,7 +567,7 @@ void ts_free_id(ts_rsrc_id id)
tsrm_mutex_unlock(tsmm_mutex);

TSRM_ERROR((TSRM_ERROR_LEVEL_CORE, "Successfully freed resource id %d", id));
}
}/*}}}*/



Expand All @@ -578,7 +578,7 @@ void ts_free_id(ts_rsrc_id id)

/* Obtain the current thread id */
TSRM_API THREAD_T tsrm_thread_id(void)
{
{/*{{{*/
#ifdef TSRM_WIN32
return GetCurrentThreadId();
#elif defined(GNUPTH)
Expand All @@ -594,12 +594,12 @@ TSRM_API THREAD_T tsrm_thread_id(void)
#elif defined(BETHREADS)
return find_thread(NULL);
#endif
}
}/*}}}*/


/* Allocate a mutex */
TSRM_API MUTEX_T tsrm_mutex_alloc(void)
{
{/*{{{*/
MUTEX_T mutexp;
#ifdef TSRM_WIN32
mutexp = malloc(sizeof(CRITICAL_SECTION));
Expand All @@ -625,12 +625,12 @@ TSRM_API MUTEX_T tsrm_mutex_alloc(void)
printf("Mutex created thread: %d\n",mythreadid());
#endif
return( mutexp );
}
}/*}}}*/


/* Free a mutex */
TSRM_API void tsrm_mutex_free(MUTEX_T mutexp)
{
{/*{{{*/
if (mutexp) {
#ifdef TSRM_WIN32
DeleteCriticalSection(mutexp);
Expand All @@ -654,15 +654,15 @@ TSRM_API void tsrm_mutex_free(MUTEX_T mutexp)
#ifdef THR_DEBUG
printf("Mutex freed thread: %d\n",mythreadid());
#endif
}
}/*}}}*/


/*
Lock a mutex.
A return value of 0 indicates success
*/
TSRM_API int tsrm_mutex_lock(MUTEX_T mutexp)
{
{/*{{{*/
TSRM_ERROR((TSRM_ERROR_LEVEL_INFO, "Mutex locked thread: %ld", tsrm_thread_id()));
#ifdef TSRM_WIN32
EnterCriticalSection(mutexp);
Expand All @@ -686,15 +686,15 @@ TSRM_API int tsrm_mutex_lock(MUTEX_T mutexp)
return acquire_sem(mutexp->sem);
return 0;
#endif
}
}/*}}}*/


/*
Unlock a mutex.
A return value of 0 indicates success
*/
TSRM_API int tsrm_mutex_unlock(MUTEX_T mutexp)
{
{/*{{{*/
TSRM_ERROR((TSRM_ERROR_LEVEL_INFO, "Mutex unlocked thread: %ld", tsrm_thread_id()));
#ifdef TSRM_WIN32
LeaveCriticalSection(mutexp);
Expand All @@ -718,50 +718,50 @@ TSRM_API int tsrm_mutex_unlock(MUTEX_T mutexp)
return release_sem(mutexp->sem);
return 0;
#endif
}
}/*}}}*/

/*
Changes the signal mask of the calling thread
*/
#ifdef HAVE_SIGPROCMASK
TSRM_API int tsrm_sigmask(int how, const sigset_t *set, sigset_t *oldset)
{
{/*{{{*/
TSRM_ERROR((TSRM_ERROR_LEVEL_INFO, "Changed sigmask in thread: %ld", tsrm_thread_id()));
/* TODO: add support for other APIs */
#ifdef PTHREADS
return pthread_sigmask(how, set, oldset);
#else
return sigprocmask(how, set, oldset);
#endif
}
}/*}}}*/
#endif


TSRM_API void *tsrm_set_new_thread_begin_handler(tsrm_thread_begin_func_t new_thread_begin_handler)
{
{/*{{{*/
void *retval = (void *) tsrm_new_thread_begin_handler;

tsrm_new_thread_begin_handler = new_thread_begin_handler;
return retval;
}
}/*}}}*/


TSRM_API void *tsrm_set_new_thread_end_handler(tsrm_thread_end_func_t new_thread_end_handler)
{
{/*{{{*/
void *retval = (void *) tsrm_new_thread_end_handler;

tsrm_new_thread_end_handler = new_thread_end_handler;
return retval;
}
}/*}}}*/


TSRM_API void *tsrm_set_shutdown_handler(tsrm_shutdown_func_t shutdown_handler)
{
{/*{{{*/
void *retval = (void *) tsrm_shutdown_handler;

tsrm_shutdown_handler = shutdown_handler;
return retval;
}
}/*}}}*/


/*
Expand All @@ -770,7 +770,7 @@ TSRM_API void *tsrm_set_shutdown_handler(tsrm_shutdown_func_t shutdown_handler)

#if TSRM_DEBUG
int tsrm_error(int level, const char *format, ...)
{
{/*{{{*/
if (level<=tsrm_error_level) {
va_list args;
int size;
Expand All @@ -785,12 +785,12 @@ int tsrm_error(int level, const char *format, ...)
} else {
return 0;
}
}
}/*}}}*/
#endif


void tsrm_error_set(int level, char *debug_filename)
{
{/*{{{*/
tsrm_error_level = level;

#if TSRM_DEBUG
Expand All @@ -807,16 +807,25 @@ void tsrm_error_set(int level, char *debug_filename)
tsrm_error_file = stderr;
}
#endif
}
}/*}}}*/

TSRM_API void *tsrm_get_ls_cache(void)
{
{/*{{{*/
return tsrm_tls_get();
}
}/*}}}*/

TSRM_API uint8_t tsrm_is_main_thread(void)
{
{/*{{{*/
return in_main_thread;
}
}/*}}}*/

#endif /* ZTS */

/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
* vim600: sw=4 ts=4 fdm=marker
* vim<600: sw=4 ts=4
*/
9 changes: 9 additions & 0 deletions TSRM/TSRM.h
Original file line number Diff line number Diff line change
Expand Up @@ -216,3 +216,12 @@ TSRM_API uint8_t tsrm_is_main_thread(void);
#endif /* ZTS */

#endif /* TSRM_H */

/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
* vim600: sw=4 ts=4 fdm=marker
* vim<600: sw=4 ts=4
*/
9 changes: 9 additions & 0 deletions TSRM/tsrm_config.w32.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,12 @@
#include <crtdbg.h>

#endif

/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
* vim600: sw=4 ts=4 fdm=marker
* vim<600: sw=4 ts=4
*/
9 changes: 9 additions & 0 deletions TSRM/tsrm_config_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,12 @@ char *alloca ();
#endif

#endif /* TSRM_CONFIG_COMMON_H */

/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
* vim600: sw=4 ts=4 fdm=marker
* vim<600: sw=4 ts=4
*/

0 comments on commit 6a3a2bf

Please sign in to comment.