Ejemplo n.º 1
0
/*
  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);
	return 0;
#elif defined(GNUPTH)
	if (pth_mutex_acquire(mutexp, 0, NULL)) {
		return 0;
	}
	return -1;
#elif defined(PTHREADS)
	return pthread_mutex_lock(mutexp);
#elif defined(NSAPI)
	crit_enter(mutexp);
	return 0;
#elif defined(PI3WEB)
	return PISync_lock(mutexp);
#elif defined(TSRM_ST)
	return st_mutex_lock(mutexp);
#elif defined(BETHREADS)
	if (atomic_add(&mutexp->ben, 1) != 0)
		return acquire_sem(mutexp->sem);
	return 0;
#endif
}
Ejemplo n.º 2
0
void task4(void* arg)
{
	for (long i = 0; i < 100000; ++i) {
		int rc = 0;
		if ((rc = st_mutex_lock(test4_mutex)) != 0) {
			std::cout << "st_mutex_lock " << rc << " " << strerror(rc) << std::endl;
			exit(1);
		}
		if ((rc = st_mutex_unlock(test4_mutex)) != 0) {
			std::cout << "st_mutex_unlock " << rc << " " << strerror(rc) << std::endl;
			exit(1);
		}
	}
}
Ejemplo n.º 3
0
CAMLprim value caml_mutex_lock(value wrapper)     /* ML */
{
  st_mutex mut = Mutex_val(wrapper);
  st_retcode retcode;

  /* PR#4351: first try to acquire mutex without releasing the master lock */
  if (st_mutex_trylock(mut) == PREVIOUSLY_UNLOCKED) return Val_unit;
  /* If unsuccessful, block on mutex */
  Begin_root(wrapper)           /* prevent the deallocation of mutex */
    enter_blocking_section();
    retcode = st_mutex_lock(mut);
    leave_blocking_section();
  End_roots();
  st_check_error(retcode, "Mutex.lock");
  return Val_unit;
}
Ejemplo n.º 4
0
/*
  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);
	return 0;
#elif defined(GNUPTH)
	if (pth_mutex_acquire(mutexp, 0, NULL)) {
		return 0;
	}
	return -1;
#elif defined(PTHREADS)
	return pthread_mutex_lock(mutexp);
#elif defined(TSRM_ST)
	return st_mutex_lock(mutexp);
#endif
}/*}}}*/
Ejemplo n.º 5
0
void task15(void* arg)
{
	st_netfd_t rfd = st_open("/Users/vss/projects/1.gz", O_RDONLY, S_IRUSR);
	st_netfd_t wfd = st_open("/Users/vss/projects/tmp.gz", O_WRONLY, S_IWUSR);

	for (long w = 0; w < 3; ++w) {
		char buffer[32096];
		const char str[] = "Of course, take it!";
		char response[1024 + sizeof(str)];
		int rc = 0;

		int c = 0;
		while (c < sizeof(buffer))
		{
			if ((rc = st_read(rfd, buffer + c, std::min<int>(4, sizeof(buffer) - c), -1)) <= 0) {
				std::cout << "st_read " << rc << " " << strerror(rc) << std::endl;
				exit(1);
			}
			c += rc;
		}

		if ((rc = st_mutex_lock(test4_mutex)) != 0) {
			std::cout << "st_mutex_lock " << rc << " " << strerror(rc) << std::endl;
			exit(1);
		}

		for (int i = 0; i < sizeof(response); i += sizeof(str))
		{
			memcpy(response + i, str, sizeof(str));
		}

		if ((rc = st_mutex_unlock(test4_mutex)) != 0) {
			std::cout << "st_mutex_unlock " << rc << " " << strerror(rc) << std::endl;
			exit(1);
		}

		if ((rc = st_write(wfd, response, sizeof(response), -1)) <= 0) {
			std::cout << "st_write " << rc << " " << strerror(rc) << std::endl;
			exit(1);
		}
	}

	st_netfd_close(rfd);
	st_netfd_close(wfd);
}
Ejemplo n.º 6
0
static void caml_io_mutex_lock(struct channel *chan)
{
  st_mutex mutex = chan->mutex;

  if (mutex == NULL) {
    st_mutex_create(&mutex);
    chan->mutex = mutex;
  }
  /* PR#4351: first try to acquire mutex without releasing the master lock */
  if (st_mutex_trylock(mutex) == PREVIOUSLY_UNLOCKED) {
    st_tls_set(last_channel_locked_key, (void *) chan);
    return;
  }
  /* If unsuccessful, block on mutex */
  enter_blocking_section();
  st_mutex_lock(mutex);
  /* Problem: if a signal occurs at this point,
     and the signal handler raises an exception, we will not
     unlock the mutex.  The alternative (doing the setspecific
     before locking the mutex is also incorrect, since we could
     then unlock a mutex that is unlocked or locked by someone else. */
  st_tls_set(last_channel_locked_key, (void *) chan);
  leave_blocking_section();
}