Пример #1
0
int sexe_event_remove(lua_State *L, int e_type, char *e_name)
{
  sexe_event_t t_event;
  sexe_event_t *e;
  shkey_t *key;
  int err;

  memset(&t_event, 0, sizeof(t_event));
  t_event.event_type = e_type;
  strncpy(t_event.mod_name, e_name, sizeof(t_event.mod_name) - 1);
  key = shkey_bin(&t_event, sizeof(sexe_event_t));

  /* remove event's global callback */
  lua_pushnil(L); 
  lua_setglobal(L, shkey_hex(key));

  e = (sexe_event_t *)shmap_get_ptr(event_map, key);
  if (e) {
    shmap_unset(event_map, key);
    free(e);
  }
  shkey_free(&key);

  return (0);
}
Пример #2
0
int shlock_close(shkey_t *key)
{
  shmap_t *lock_map = get_shlock_map();
  shlock_t *lk;
  pid_t tid;
  int err;

  lk = shmap_get_ptr(lock_map, key);
  if (!lk)
    return (0); /* all done */

  if (lk->ref == 0)
    return (0); /* the mutex is not locked. */

  tid  = gettid();
  if (tid != lk->tid)
    return (0); /* wrong thread calling. */

#ifdef USE_LIBPTHREAD
  err = pthread_mutex_unlock(&lk->mutex);
  if (err) {
    return (-1);
  }
#endif

  /* assign variables after mutex is unlocked. */
  lk->ref--;
  if (lk->ref == 0)
    lk->tid = 0;

  return (0);
}
Пример #3
0
int shlock_tryopen(shkey_t *key, int flags, shlock_t **lock_p)
{
  shmap_t *lock_map = get_shlock_map();
  shlock_t *lk;
  pid_t tid;
#ifdef USE_LIBPTHREAD
  pthread_mutexattr_t attr;
  int err;
#endif

  tid = gettid();
  lk = shmap_get_ptr(lock_map, key);
  if (!lk) {
    lk = (shlock_t *)calloc(1, sizeof(shlock_t));
    if (!lk)
      return (-1);
    shmap_set_ptr(lock_map, key, lk);
#ifdef USE_LIBPTHREAD
    memset(&attr, 0, sizeof(attr));
    if (!(flags & SHLK_PRIVATE)) {
      pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
}
    pthread_mutex_init(&lk->mutex, &attr);
#endif
  } else if (lk->ref) {
    if ((flags & SHLK_PRIVATE)) {
      /* mutex is already locked. */
      return (1);
    }
    if (tid != lk->tid)
      return (1); /* lock is not accessible from this thread. */
  }

  if (!lk->ref || lk->tid != tid) {
#ifdef USE_LIBPTHREAD
    /* returns an errno */
    err = pthread_mutex_trylock(&lk->mutex);
    if (err == EBUSY)
      return (1);
    if (err)
      return (-1);
#endif
  }

  /* assign variables after mutex is locked. */
  lk->tid = tid;
  lk->ref++;
  *lock_p = lk;

  return (0);
}
Пример #4
0
shlock_t *shlock_open(shkey_t *key, int flags)
{
  shmap_t *lock_map = get_shlock_map();
  shlock_t *lk;
  pid_t tid;
#ifdef USE_LIBPTHREAD
  pthread_mutexattr_t attr;
  int err;
#endif

  tid = gettid();
  lk = shmap_get_ptr(lock_map, key);
  if (!lk) {
    lk = (shlock_t *)calloc(1, sizeof(shlock_t));
    if (!lk)
      return (NULL);
    shmap_set_ptr(lock_map, key, lk);
#ifdef USE_LIBPTHREAD
    memset(&attr, 0, sizeof(attr));
    if (!(flags & SHLK_PRIVATE)) {
      pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
}
    pthread_mutex_init(&lk->mutex, &attr);
#endif
  } 

#ifdef USE_LIBPTHREAD
  err = pthread_mutex_lock(&lk->mutex);
  if (err) {
    return (NULL);
  }
#else
  /* bug: acts like trylock() instead of lock() .. need to introduce waiting psuedo-lock */
  if (lk->ref) {
    if ((flags & SHLK_PRIVATE)) {
      /* mutex is already locked. */
      return (NULL);
    }
    if (tid != lk->tid) {
      return (NULL); /* lock is not accessible from this thread. */
    }
  }
#endif

  /* assign variables after mutex is locked. */
  lk->tid = tid;
  lk->ref++;

  return (lk);
}