Beispiel #1
0
inline static int uv__rwlock_fallback_tryrdlock(uv_rwlock_t* rwlock) {
  int err;

  err = uv_mutex_trylock(&rwlock->fallback_.read_mutex_);
  if (err)
    goto out;

  err = 0;
  if (rwlock->fallback_.num_readers_ == 0)
    err = uv_mutex_trylock(&rwlock->fallback_.write_mutex_);

  if (err == 0)
    rwlock->fallback_.num_readers_++;

  uv_mutex_unlock(&rwlock->fallback_.read_mutex_);

out:
  return err;
}
Beispiel #2
0
inline static int uv__rwlock_fallback_tryrdlock(uv_rwlock_t* rwlock) {
  int ret;

  ret = -1;

  if (uv_mutex_trylock(&rwlock->fallback_.read_mutex_)) goto out;

  if (rwlock->fallback_.num_readers_ == 0)
    ret = uv_mutex_trylock(&rwlock->fallback_.write_mutex_);
  else
    ret = 0;

  if (ret == 0) rwlock->fallback_.num_readers_++;

  uv_mutex_unlock(&rwlock->fallback_.read_mutex_);

out:
  return ret;
}
Beispiel #3
0
static PyObject *
Mutex_func_trylock(Mutex *self)
{
    int r;

    RAISE_IF_NOT_INITIALIZED(self, NULL);

    Py_BEGIN_ALLOW_THREADS
    r = uv_mutex_trylock(&self->uv_mutex);
    Py_END_ALLOW_THREADS

    return PyBool_FromLong((long)(r == 0));
}
Beispiel #4
0
static int uv__custom_sem_trywait(uv_sem_t* sem_) {
  uv_semaphore_t* sem;

  sem = *(uv_semaphore_t**)sem_;
  if (uv_mutex_trylock(&sem->mutex) != 0)
    return UV_EAGAIN;

  if (sem->value == 0) {
    uv_mutex_unlock(&sem->mutex);
    return UV_EAGAIN;
  }

  sem->value--;
  uv_mutex_unlock(&sem->mutex);

  return 0;
}
Beispiel #5
0
static mrb_value
mrb_uv_mutex_trylock(mrb_state *mrb, mrb_value self)
{
  return mrb_bool_value(uv_mutex_trylock((uv_mutex_t*)mrb_uv_get_ptr(mrb, self, &mrb_uv_mutex_type)));
}
Beispiel #6
0
inline static int uv__rwlock_fallback_trywrlock(uv_rwlock_t* rwlock) {
  return uv_mutex_trylock(&rwlock->fallback_.write_mutex_);
}
Beispiel #7
0
int on_url (http_parser* parser, const char *at, size_t length)
{
	client_t* client = (client_t*) parser->data;
	if(parser->method != 1)
		return 0;
	LOGF ("[ %5d ] url parsed", client->request_num);
	struct http_parser_url *u = malloc (sizeof (struct http_parser_url) );
	uv_tcp_t* handle = &client->handle;
	char* path = malloc ((length +1)* sizeof (char) );
	char * content = malloc (304800);
	char * buffer = malloc (304800);
	uv_buf_t resbuf;
	strncpy (path, at, length);
	size_t user_id=server_param->model.parameters.users_number;
	path[length]=0;
	sscanf (path, "/user=%u", &user_id);
	
	if (user_id >= server_param->model.parameters.users_number)
	{
		strcpy (content, "{ \n"\
		        "\"error\": { \n"\
		        "\"message\": \"Unsupported get request.\", \n"\
		        "\"type\": \"User Not Valid\", \n"\
		        "}\n"\
		        "}");
	}
	else
	{
		rating_estimator_parameters_t* estim_param = malloc (sizeof (rating_estimator_parameters_t) );
		int backup=0;
		if (uv_mutex_trylock(&factors_mutex))
		{
		uv_mutex_lock(&factors_backup_mutex);
		backup=1;
		estim_param->lfactors = factors;
		printf("Backup \n");
	        printf("factors_backup_mutex locked outside of thread \n");
		}else
		{
		estim_param->lfactors = factors_backup;
		}
		estim_param->tset = training_set;
		estim_param->user_index = user_id;
		recommended_items_t* rec = recommend_items (estim_param, server_param->model, 4);
		uv_mutex_unlock(&factors_mutex);
		if(backup==1)
		{
			uv_mutex_unlock(&factors_backup_mutex);
		}
		LOG ("recommendation completed");
		int i;
		sprintf (content, "{ \n \"user_index\" : %u, \n",user_id);
		
		sprintf (content, "%s \"recommended_items\" : [ \n",content);
		for (i = 0; i < rec->items_number - 1; i++)
		{
			sprintf (content, "%s { \"item\" : %u , \"rating\" : %lf }, \n", content, rec->items[i].index, rec->items[i].rating);
		}
		sprintf (content, "%s { \"item\" : %u , \"rating\" : %lf } \n ], \n", content, rec->items[rec->items_number - 1].index, rec->items[rec->items_number - 1].rating);
		coo_matrix_t* top_rated_items=get_top_rated_items(training_set,user_id,6);
		sprintf (content, "%s \"top_rated_items\" : [ \n",content);
		for(i=0;i < top_rated_items->size - 1; i++)
		{
			sprintf (content, "%s { \"item\" : %u , \"rating\" : %lf }, \n", 
				 content, top_rated_items->entries[i].row_i, top_rated_items->entries[i].value);
		}
			sprintf (content, "%s { \"item\" : %u , \"rating\" : %lf } \n ], \n", 
				 content, top_rated_items->entries[top_rated_items->size-1].row_i, top_rated_items->entries[top_rated_items->size-1].value);
		coo_matrix_t* rated_items=get_rated_items(training_set,user_id);
		sprintf (content, "%s \"rated_items\" : [ \n",content);
		for(i=0;i < rated_items->current_size; i++)
		{
			sprintf (content, "%s { \"item\" : %u , \"rating\" : %lf }, \n", 
				 content, rated_items->entries[i].row_i, rated_items->entries[i].value);
		}
			sprintf (content, "%s { \"item\" : %u , \"rating\" : %lf } \n ] \n", 
				 content, rated_items->entries[rated_items->current_size-1].row_i, rated_items->entries[rated_items->current_size-1].value);
		sprintf (content, "%s  \n }", content);
		free_coo_matrix(rated_items);
		free_coo_matrix(top_rated_items);
		
	}
		sprintf (buffer, "%s %u \r\n\r\n%s\n", HEADER, strlen (content), content);

		resbuf.base = buffer;
		resbuf.len = strlen (resbuf.base);
		
		uv_write (
	    &client->write_req,
	    (uv_stream_t*) &client->handle,
	    &resbuf,
	    1,
	    after_write);
	free (content);
	free (path);
	return 0;
}
Beispiel #8
0
 bool Mutex::trylock()
 {
     int r = uv_mutex_trylock(&mutex);
     return r == 0;
 }