bool Mutex::try_lock() { // 単に他がロックしている時もTryLockが失敗するのでSDL_GetErrorによるエラーチェックはしない auto res = SDL_TryLockMutex(_mutex); // TryLockが失敗した時に後のエラーチェックに響くため、ここでリセット SDL_ClearError(); return res == 0; }
static void qtvlist_find_player_cmd(void) { qbool list_all = false; if (qtvlist_mutex == NULL) { Com_Printf("error: cannot read QTV list, mutex not initialized\n"); return; } if (Cmd_Argc() == 1) { list_all = true; } else if (Cmd_Argc() > 2) { Com_Printf("usage: find [nickname] (empty arg lists all)\n"); return; } if (SDL_TryLockMutex(qtvlist_mutex) != 0) { Com_Printf("Player list is being updated, please try again soon\n"); return; } if (list_all) { qtvlist_find_player("", true); } else { qtvlist_find_player(Cmd_Argv(1), false); } SDL_UnlockMutex(qtvlist_mutex); }
int get_from_queue_nonblocking(threadsafe_queue *tq, void *item) { int retval=false; if (SDL_TryLockMutex(tq->mutex) == SDL_MUTEX_TIMEDOUT) return false; if (tq->head != tq->tail) { memcpy(item, tq->data + tq->itemsize * tq->tail, tq->itemsize); ++tq->tail; if (tq->tail >= tq->count) tq->tail = 0; retval = true; } SDL_UnlockMutex(tq->mutex); return retval; }
/********************************************************* Wake-up NPC. Execute it's AI script immediatly return -1 on error *********************************************************/ int character_wake_up(const char * id) { context_t * ctx; ctx = context_find(id); /* Wake up NPC */ ctx->next_execution_time = 0; if( SDL_TryLockMutex (ctx->cond_mutex) == 0 ) { SDL_CondSignal (ctx->cond); SDL_UnlockMutex (ctx->cond_mutex); } return 0; }
static mrb_value mrb_sdl2_mutex_try_lock(mrb_state *mrb, mrb_value self) { mrb_sdl2_mutex_data_t *data = (mrb_sdl2_mutex_data_t*)mrb_data_get_ptr(mrb, self, &mrb_sdl2_mutex_data_type); if (NULL != data->mutex) { int const status = SDL_TryLockMutex(data->mutex); if (0 == status) { return mrb_true_value(); } if (SDL_MUTEX_TIMEDOUT == status) { return mrb_false_value(); } mruby_sdl2_raise_error(mrb); } return self; }
/***************************** Kick a character out of the game It does not disconnect it. An NPC could re pop from an out of game state. A player can go back in game after choosing a new character id return -1 if fails *****************************/ int character_out_of_game( const char * id) { context_t * ctx; werr(LOGDEBUG,"Kicking %s out of the game",id); ctx = context_find(id); context_set_in_game(ctx,false); context_spread(ctx); if( context_is_npc(ctx) == true ) { /* Wake up NPC */ if( SDL_TryLockMutex (ctx->cond_mutex) == 0 ) { SDL_CondSignal (ctx->cond); SDL_UnlockMutex (ctx->cond_mutex); } } return 0; }
void qtvlist_joinfromqtv_cmd(void) { /* FIXME: Make this prettier */ char addr[512]; char httpaddr[512]; char gameaddress[512]; char *currstream, *server; currstream = CL_QTV_GetCurrentStream(); if (currstream == NULL) { Com_Printf("Not connected to a QTV, can't join\n"); return; } strlcpy(&addr[0], currstream, sizeof(addr)); /* Bleh, transformation of id@server:port to http://server:port/watch.qtv?sid=id */ server = strchr(&addr[0], '@'); if (server == NULL) { Com_Printf("error: wrong format on input\n"); return; } *server++ = 0; snprintf(&httpaddr[0], sizeof(httpaddr), "http://%s/watch.qtv?sid=%s", server, &addr[0]); if (SDL_TryLockMutex(qtvlist_mutex) != 0) { Com_Printf("qtvlist is being updated, please try again soon\n"); return; } qtvlist_get_gameaddress((const char*)&httpaddr[0], &gameaddress[0], sizeof(gameaddress)); SDL_UnlockMutex(qtvlist_mutex); if (gameaddress[0] != 0) { Cbuf_AddText(va("connect %s\n", &gameaddress[0])); } else { Com_Printf("No game address found for this QTV stream\n"); } }
/***************************** Disconnect a character. This kill a NPC AI thread return -1 if fails *****************************/ int character_disconnect(const char * id) { context_t * ctx; werr(LOGDEVELOPER, "Disconnecting %s", id); ctx = context_find(id); context_set_in_game(ctx, false); context_set_connected(ctx, false); context_spread(ctx); if (context_is_npc(ctx) == true) { /* Wake up NPC */ if (SDL_TryLockMutex(ctx->cond_mutex) == 0) { SDL_CondSignal(ctx->cond); SDL_UnlockMutex(ctx->cond_mutex); } } return 0; }
static int qtvlist_update(void *unused) { char *jsondata = NULL; int ret = -1; int res; (void)unused; res = SDL_TryLockMutex(qtvlist_mutex); if (res == SDL_MUTEX_TIMEDOUT) { Com_Printf("The qtvlist is already in the process of being updated\n"); goto out; } else if (res < 0) { Com_Printf("error: mutex lock failed (SDL2): %s\n", SDL_GetError()); goto out; } jsondata = qtvlist_get_jsondata(); if (jsondata == NULL) { goto out; } qtvlist_json_load_and_verify_string(jsondata); Q_free(jsondata); if (root == NULL) { goto out; } ret = 0; out: if (res == 0) { SDL_UnlockMutex(qtvlist_mutex); } return ret; }
bool vsMutex::TryLock() { return SDL_TRUE == SDL_TryLockMutex(m_mutex); }
bool fe_mt_mutex_try_lock(fe_mt_mutex *mutex) { return !SDL_TryLockMutex(*mutex); }
SDLX::Mutex :: MutexStatus SDLX::Mutex :: TryLock () { return static_cast <MutexStatus> ( SDL_TryLockMutex ( MHandle ) ); }
def_dll int sdl_mutex::try_lock() { return SDL_TryLockMutex(_mutex); }
static void qtvlist_qtv_cmd(void) { char tmp[256] = {0}; char *port; const char *qtvaddress; extern qbool connected_via_proxy; if (qtvlist_mutex == NULL) { Com_Printf("error: cannot read QTV list, mutex not initialized\n"); return; } if (Cmd_Argc() < 2) { /* No argument, use current connected server ip:port */ if (cls.state < ca_connected) { Com_Printf("error: not connected to a server\n"); return; } /* FIXME: It's pretty ugly, refactor all this so that we can for sure * keep track of where we are connected, what kind of endpoint it is and * also which server we asked the proxy to connect to... */ if (connected_via_proxy) { /* If connected through proxy, the target server is * hopefully still in userinfo/prx */ char *prx = Info_ValueForKey(cls.userinfo, "prx"); char *srv = strrchr(prx, '@'); if (srv) { srv++; } else { srv = prx; } if (!prx[0]) { Com_Printf("error: connected through proxy but missing 'prx' userinfo, can't find QTV stream\n"); return; } strlcpy(&tmp[0], srv, sizeof(tmp)); } else { strlcpy(&tmp[0], NET_AdrToString(cls.server_adr), sizeof(tmp)); } } else if (Cmd_Argc() == 2) { /* User provided which qwserver to find a QTV stream address for */ strlcpy(&tmp[0], Cmd_Argv(1), sizeof(tmp)); } else { Com_Printf("usage: %s [qwserver:port]\n", Cmd_Argv(0)); return; } port = strchr(&tmp[0], ':'); if (port == NULL) { port = "27500"; } else { *port = 0; port++; } if (SDL_TryLockMutex(qtvlist_mutex) != 0) { Com_Printf("QTV list is being updated, please try again soon\n"); return; } qtvaddress = qtvlist_get_qtvaddress((const char*)&tmp[0], Q_atoi(port)); if (qtvaddress != NULL) { Cbuf_AddText("qtvplay "); Cbuf_AddText(qtvaddress); Cbuf_AddText("\n"); } else { Com_Printf("No QTV stream address found for '%s:%s'\n", &tmp[0], port); } SDL_UnlockMutex(qtvlist_mutex); }
bool ZL_Mutex::TryLock() { return (SDL_TryLockMutex(impl->mutex)==0); }
bool ZL_MutexTryLock(ZL_MutexHandle mutex) { return (SDL_TryLockMutex((SDL_mutex*)mutex)==0); }