Skip to content

Commit

Permalink
refactor: Unify local and remote implementation
Browse files Browse the repository at this point in the history
The functions of local and remote interface should be unified
to keep consistency.

The modification includes function unification and adding the hardware
to the candidate list if the initialization is success.
  • Loading branch information
marktwtn committed Nov 2, 2019
1 parent c353741 commit 076d280
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 13 deletions.
36 changes: 25 additions & 11 deletions src/dcurl.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ static bool isInitialized = false;
static uv_sem_t notify;

LIST_HEAD(IMPL_LIST);
LIST_HEAD(REMOTE_IMPL_LIST);

#if defined(ENABLE_AVX)
extern ImplContext PoWAVX_Context;
Expand Down Expand Up @@ -79,7 +80,7 @@ bool dcurl_init()
#endif

#if defined(ENABLE_REMOTE)
ret &= initializeRemoteContext(&Remote_Context);
ret &= registerRemoteContext(&Remote_Context);
uv_sem_init(&notify_remote, 0);
#endif

Expand All @@ -93,7 +94,14 @@ void dcurl_destroy()
struct list_head *p;

#if defined(ENABLE_REMOTE)
destroyRemoteContext(&Remote_Context);
RemoteImplContext *remoteImpl = NULL;
struct list_head *pRemote;

list_for_each (pRemote, &REMOTE_IMPL_LIST) {
remoteImpl = list_entry(pRemote, RemoteImplContext, list);
destroyRemoteContext(remoteImpl);
list_del(pRemote);
}
#endif

list_for_each (p, &IMPL_LIST) {
Expand All @@ -116,30 +124,36 @@ int8_t *dcurl_entry(int8_t *trytes, int mwm, int threads)
return NULL;

#if defined(ENABLE_REMOTE)
RemoteImplContext *remoteImpl = NULL;
struct list_head *pRemote;

do {
if (enterRemoteContext(&Remote_Context)) {
pow_ctx = getRemoteContext(&Remote_Context, trytes, mwm);
goto remote_pow;
list_for_each (pRemote, &REMOTE_IMPL_LIST) {
remoteImpl = list_entry(pRemote, RemoteImplContext, list);
if (enterRemoteContext(remoteImpl)) {
pow_ctx = getRemoteContext(remoteImpl, trytes, mwm);
goto remote_pow;
}
}
uv_sem_wait(&notify_remote);
} while (1);

remote_pow:
if (!doRemoteContext(&Remote_Context, pow_ctx)) {
if (!doRemoteContext(remoteImpl, pow_ctx)) {
/* The remote interface can not work without activated RabbitMQ broker
* and remote worker. If it is not working, the PoW would be calculated
* by the local machine. And the remote interface resource should be
* released.
*/
freeRemoteContext(&Remote_Context, pow_ctx);
exitRemoteContext(&Remote_Context);
freeRemoteContext(remoteImpl, pow_ctx);
exitRemoteContext(remoteImpl);
uv_sem_post(&notify_remote);
goto local_pow;
} else {
res = getRemoteResult(&Remote_Context, pow_ctx);
res = getRemoteResult(remoteImpl, pow_ctx);
}
freeRemoteContext(&Remote_Context, pow_ctx);
exitRemoteContext(&Remote_Context);
freeRemoteContext(remoteImpl, pow_ctx);
exitRemoteContext(remoteImpl);
uv_sem_post(&notify_remote);
return res;

Expand Down
6 changes: 4 additions & 2 deletions src/implcontext.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ extern struct list_head IMPL_LIST;

bool registerImplContext(ImplContext *impl_ctx)
{
list_add(&impl_ctx->list, &IMPL_LIST);
return initializeImplContext(impl_ctx);
bool res = initializeImplContext(impl_ctx);
if (res)
list_add(&impl_ctx->list, &IMPL_LIST);
return res;
}

bool initializeImplContext(ImplContext *impl_ctx)
Expand Down
10 changes: 10 additions & 0 deletions src/remote_interface.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@
#include <string.h>
#include "trinary.h"

extern struct list_head REMOTE_IMPL_LIST;

bool registerRemoteContext(RemoteImplContext *remote_ctx)
{
bool res = initializeRemoteContext(remote_ctx);
if (res)
list_add(&remote_ctx->list, &REMOTE_IMPL_LIST);
return res;
}

bool initializeRemoteContext(RemoteImplContext *remote_ctx)
{
bool res = remote_ctx->initialize(remote_ctx);
Expand Down
5 changes: 5 additions & 0 deletions src/remote_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <stdint.h>
#include "common.h"
#include "constants.h"
#include "list.h"
#include "remote_common.h"
#include "uv.h"

Expand Down Expand Up @@ -55,8 +56,12 @@ struct _remote_impl_context {
int8_t *(*getPoWResult)(void *pow_ctx);
PoW_Info (*getPoWInfo)(void *pow_ctx);
bool (*freePoWContext)(RemoteImplContext *remote_ctx, void *pow_ctx);

/* Linked list */
struct list_head list;
};

bool registerRemoteContext(RemoteImplContext *remote_ctx);
bool initializeRemoteContext(RemoteImplContext *remote_ctx);
void destroyRemoteContext(RemoteImplContext *remote_ctx);
bool enterRemoteContext(RemoteImplContext *remote_ctx);
Expand Down

0 comments on commit 076d280

Please sign in to comment.