Skip to content

Commit

Permalink
feat: Make thread synchronization portable
Browse files Browse the repository at this point in the history
Replace the thread synchronization functions with the portable
libtuv APIs and remove the unused code.

TODO:
Make mutex of src/compat-ccurl.c portable.

Close #107.
  • Loading branch information
marktwtn committed Mar 28, 2019
1 parent c8a6b25 commit db6b6f9
Show file tree
Hide file tree
Showing 11 changed files with 39 additions and 71 deletions.
32 changes: 5 additions & 27 deletions src/dcurl.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#endif
#include "implcontext.h"
#include "trinary.h"
#include "uv.h"
#if defined(ENABLE_AVX)
#include "pow_avx.h"
#elif defined(ENABLE_SSE)
Expand All @@ -25,20 +26,9 @@
#include "pow_c.h"
#endif

#ifdef __APPLE__
#include <dispatch/dispatch.h>
#else
#include <semaphore.h>
#endif

/* check whether dcurl is initialized */
static bool isInitialized = false;

#ifdef __APPLE__
static dispatch_semaphore_t notify;
#else
static sem_t notify;
#endif
static uv_sem_t notify;

LIST_HEAD(IMPL_LIST);

Expand Down Expand Up @@ -78,11 +68,7 @@ bool dcurl_init()
ret &= registerImplContext(&PoWFPGAAccel_Context);
#endif

#ifdef __APPLE__
notify = dispatch_semaphore_create(0);
#else
sem_init(&notify, 0, 0);
#endif
uv_sem_init(&notify, 0);
return isInitialized = ret;
}

Expand Down Expand Up @@ -118,11 +104,7 @@ int8_t *dcurl_entry(int8_t *trytes, int mwm, int threads)
goto do_pow;
}
}
#ifdef __APPLE__
dispatch_semaphore_wait(notify, DISPATCH_TIME_FOREVER);
#else
sem_wait(&notify);
#endif
uv_sem_wait(&notify);
} while (1);

do_pow:
Expand All @@ -133,10 +115,6 @@ int8_t *dcurl_entry(int8_t *trytes, int mwm, int threads)
}
freePoWContext(impl, pow_ctx);
exitImplContext(impl);
#ifdef __APPLE__
dispatch_semaphore_signal(notify);
#else
sem_post(&notify);
#endif
uv_sem_post(&notify);
return res;
}
10 changes: 5 additions & 5 deletions src/implcontext.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,21 @@ void destroyImplContext(ImplContext *impl_ctx)

bool enterImplContext(ImplContext *impl_ctx)
{
pthread_mutex_lock(&impl_ctx->lock);
uv_mutex_lock(&impl_ctx->lock);
if (impl_ctx->num_working_thread >= impl_ctx->num_max_thread) {
pthread_mutex_unlock(&impl_ctx->lock);
uv_mutex_unlock(&impl_ctx->lock);
return false; /* Access Failed */
}
impl_ctx->num_working_thread++;
pthread_mutex_unlock(&impl_ctx->lock);
uv_mutex_unlock(&impl_ctx->lock);
return true; /* Access Success */
}

void exitImplContext(ImplContext *impl_ctx)
{
pthread_mutex_lock(&impl_ctx->lock);
uv_mutex_lock(&impl_ctx->lock);
impl_ctx->num_working_thread--;
pthread_mutex_unlock(&impl_ctx->lock);
uv_mutex_unlock(&impl_ctx->lock);
}

void *getPoWContext(ImplContext *impl_ctx, int8_t *trytes, int mwm, int threads)
Expand Down
4 changes: 2 additions & 2 deletions src/implcontext.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#ifndef IMPL_CTX_H_
#define IMPL_CTX_H_

#include <pthread.h>
#include <stdbool.h>
#include <stdint.h>
#include "common.h"
#include "list.h"
#include "uv.h"

typedef struct _impl_context ImplContext;

Expand All @@ -14,7 +14,7 @@ struct _impl_context {
char *description;

/* Multi-thread Management */
pthread_mutex_t lock;
uv_mutex_t lock;
int bitmap; /* Used to tell which slot is available */
int num_max_thread;
int num_working_thread;
Expand Down
14 changes: 6 additions & 8 deletions src/pow_avx.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@
*/

#include "pow_avx.h"
#include <pthread.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <uv.h>
#include "cpu-utils.h"
#include "curl.h"
#include "implcontext.h"
Expand Down Expand Up @@ -610,7 +608,7 @@ static bool PoWAVX_Context_Initialize(ImplContext *impl_ctx)
uv_loop_init(&ctx[i].loop);
}
impl_ctx->context = ctx;
pthread_mutex_init(&impl_ctx->lock, NULL);
uv_mutex_init(&impl_ctx->lock);
return true;

fail:
Expand Down Expand Up @@ -643,11 +641,11 @@ static void *PoWAVX_getPoWContext(ImplContext *impl_ctx,
int mwm,
int threads)
{
pthread_mutex_lock(&impl_ctx->lock);
uv_mutex_lock(&impl_ctx->lock);
for (int i = 0; i < impl_ctx->num_max_thread; i++) {
if (impl_ctx->bitmap & (0x1 << i)) {
impl_ctx->bitmap &= ~(0x1 << i);
pthread_mutex_unlock(&impl_ctx->lock);
uv_mutex_unlock(&impl_ctx->lock);
PoW_AVX_Context *ctx =
impl_ctx->context + sizeof(PoW_AVX_Context) * i;
memcpy(ctx->input_trytes, trytes, TRANSACTION_TRYTES_LENGTH);
Expand All @@ -660,15 +658,15 @@ static void *PoWAVX_getPoWContext(ImplContext *impl_ctx,
return ctx;
}
}
pthread_mutex_unlock(&impl_ctx->lock);
uv_mutex_unlock(&impl_ctx->lock);
return NULL; /* It should not happen */
}

static bool PoWAVX_freePoWContext(ImplContext *impl_ctx, void *pow_ctx)
{
pthread_mutex_lock(&impl_ctx->lock);
uv_mutex_lock(&impl_ctx->lock);
impl_ctx->bitmap |= 0x1 << ((PoW_AVX_Context *) pow_ctx)->indexOfContext;
pthread_mutex_unlock(&impl_ctx->lock);
uv_mutex_unlock(&impl_ctx->lock);
return true;
}

Expand Down
3 changes: 1 addition & 2 deletions src/pow_avx.h
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
#ifndef POW_AVX_H_
#define POW_AVX_H_

#include <pthread.h>
#include <stdbool.h>
#include <stdint.h>
#include <uv.h>
#include "common.h"
#include "constants.h"
#include "trinary.h"
#include "uv.h"

typedef struct _pwork_struct Pwork_struct;

Expand Down
14 changes: 6 additions & 8 deletions src/pow_c.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@
*/

#include "pow_c.h"
#include <pthread.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <uv.h>
#include "cpu-utils.h"
#include "curl.h"
#include "implcontext.h"
Expand Down Expand Up @@ -374,7 +372,7 @@ static bool PoWC_Context_Initialize(ImplContext *impl_ctx)
uv_loop_init(&ctx[i].loop);
}
impl_ctx->context = ctx;
pthread_mutex_init(&impl_ctx->lock, NULL);
uv_mutex_init(&impl_ctx->lock);
return true;

fail:
Expand Down Expand Up @@ -407,11 +405,11 @@ static void *PoWC_getPoWContext(ImplContext *impl_ctx,
int mwm,
int threads)
{
pthread_mutex_lock(&impl_ctx->lock);
uv_mutex_lock(&impl_ctx->lock);
for (int i = 0; i < impl_ctx->num_max_thread; i++) {
if (impl_ctx->bitmap & (0x1 << i)) {
impl_ctx->bitmap &= ~(0x1 << i);
pthread_mutex_unlock(&impl_ctx->lock);
uv_mutex_unlock(&impl_ctx->lock);
PoW_C_Context *ctx = impl_ctx->context + sizeof(PoW_C_Context) * i;
memcpy(ctx->input_trytes, trytes, TRANSACTION_TRYTES_LENGTH);
ctx->mwm = mwm;
Expand All @@ -423,15 +421,15 @@ static void *PoWC_getPoWContext(ImplContext *impl_ctx,
return ctx;
}
}
pthread_mutex_unlock(&impl_ctx->lock);
uv_mutex_unlock(&impl_ctx->lock);
return NULL; /* It should not happen */
}

static bool PoWC_freePoWContext(ImplContext *impl_ctx, void *pow_ctx)
{
pthread_mutex_lock(&impl_ctx->lock);
uv_mutex_lock(&impl_ctx->lock);
impl_ctx->bitmap |= 0x1 << ((PoW_C_Context *) pow_ctx)->indexOfContext;
pthread_mutex_unlock(&impl_ctx->lock);
uv_mutex_unlock(&impl_ctx->lock);
return true;
}

Expand Down
3 changes: 1 addition & 2 deletions src/pow_c.h
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
#ifndef POW_C_H_
#define POW_C_H_

#include <pthread.h>
#include <stdbool.h>
#include <stdint.h>
#include <uv.h>
#include "common.h"
#include "constants.h"
#include "trinary.h"
#include "uv.h"

typedef struct _pwork_struct Pwork_struct;

Expand Down
12 changes: 6 additions & 6 deletions src/pow_cl.c
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ static bool PoWCL_Context_Initialize(ImplContext *impl_ctx)
impl_ctx->bitmap = impl_ctx->bitmap << 1 | 0x1;
}
impl_ctx->context = ctx;
pthread_mutex_init(&impl_ctx->lock, NULL);
uv_mutex_init(&impl_ctx->lock);
return true;

fail:
Expand All @@ -290,11 +290,11 @@ static void *PoWCL_getPoWContext(ImplContext *impl_ctx,
int mwm,
int threads)
{
pthread_mutex_lock(&impl_ctx->lock);
uv_mutex_lock(&impl_ctx->lock);
for (int i = 0; i < impl_ctx->num_max_thread; i++) {
if (impl_ctx->bitmap & (0x1 << i)) {
impl_ctx->bitmap &= ~(0x1 << i);
pthread_mutex_unlock(&impl_ctx->lock);
uv_mutex_unlock(&impl_ctx->lock);
PoW_CL_Context *ctx =
impl_ctx->context + sizeof(PoW_CL_Context) * i;
memcpy(ctx->input_trytes, trytes, TRANSACTION_TRYTES_LENGTH);
Expand All @@ -303,15 +303,15 @@ static void *PoWCL_getPoWContext(ImplContext *impl_ctx,
return ctx;
}
}
pthread_mutex_unlock(&impl_ctx->lock);
uv_mutex_unlock(&impl_ctx->lock);
return NULL; /* It should not happen */
}

static bool PoWCL_freePoWContext(ImplContext *impl_ctx, void *pow_ctx)
{
pthread_mutex_lock(&impl_ctx->lock);
uv_mutex_lock(&impl_ctx->lock);
impl_ctx->bitmap |= 0x1 << ((PoW_CL_Context *) pow_ctx)->indexOfContext;
pthread_mutex_unlock(&impl_ctx->lock);
uv_mutex_unlock(&impl_ctx->lock);
return true;
}

Expand Down
1 change: 0 additions & 1 deletion src/pow_fpga_accel.c
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,6 @@ static bool PoWFPGAAccel_Context_Initialize(ImplContext *impl_ctx)
ctx->cpow_map = (uint32_t *) (ctx->fpga_regs_map + CPOW_BASE);

impl_ctx->context = ctx;
pthread_mutex_init(&impl_ctx->lock, NULL);

return true;

Expand Down
14 changes: 6 additions & 8 deletions src/pow_sse.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@

#include "pow_sse.h"
#include <inttypes.h>
#include <pthread.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <uv.h>
#include "cpu-utils.h"
#include "curl.h"
#include "implcontext.h"
Expand Down Expand Up @@ -392,7 +390,7 @@ static bool PoWSSE_Context_Initialize(ImplContext *impl_ctx)
uv_loop_init(&ctx[i].loop);
}
impl_ctx->context = ctx;
pthread_mutex_init(&impl_ctx->lock, NULL);
uv_mutex_init(&impl_ctx->lock);
return true;

fail:
Expand Down Expand Up @@ -425,11 +423,11 @@ static void *PoWSSE_getPoWContext(ImplContext *impl_ctx,
int mwm,
int threads)
{
pthread_mutex_lock(&impl_ctx->lock);
uv_mutex_lock(&impl_ctx->lock);
for (int i = 0; i < impl_ctx->num_max_thread; i++) {
if (impl_ctx->bitmap & (0x1 << i)) {
impl_ctx->bitmap &= ~(0x1 << i);
pthread_mutex_unlock(&impl_ctx->lock);
uv_mutex_unlock(&impl_ctx->lock);
PoW_SSE_Context *ctx =
impl_ctx->context + sizeof(PoW_SSE_Context) * i;
memcpy(ctx->input_trytes, trytes, TRANSACTION_TRYTES_LENGTH);
Expand All @@ -442,15 +440,15 @@ static void *PoWSSE_getPoWContext(ImplContext *impl_ctx,
return ctx;
}
}
pthread_mutex_unlock(&impl_ctx->lock);
uv_mutex_unlock(&impl_ctx->lock);
return NULL; /* It should not happen */
}

static bool PoWSSE_freePoWContext(ImplContext *impl_ctx, void *pow_ctx)
{
pthread_mutex_lock(&impl_ctx->lock);
uv_mutex_lock(&impl_ctx->lock);
impl_ctx->bitmap |= 0x1 << ((PoW_SSE_Context *) pow_ctx)->indexOfContext;
pthread_mutex_unlock(&impl_ctx->lock);
uv_mutex_unlock(&impl_ctx->lock);
return true;
}

Expand Down
3 changes: 1 addition & 2 deletions src/pow_sse.h
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
#ifndef POW_SSE_H_
#define POW_SSE_H_

#include <pthread.h>
#include <stdbool.h>
#include <stdint.h>
#include <uv.h>
#include "common.h"
#include "constants.h"
#include "trinary.h"
#include "uv.h"

typedef struct _pwork_struct Pwork_struct;

Expand Down

0 comments on commit db6b6f9

Please sign in to comment.