Example #1
0
/**
 * Acquire existing session from sotrage or create new one.
 * All session MUST be requested through this function.
 * @param[in] ip IPv4 address of client.
 * @param[in] existing_only Do only existing sssion search.
 * @return New client.
 */
struct zsession *session_acquire(uint32_t ip, bool existing_only)
{
    struct zsession *sess = NULL;
    size_t sidx = STORAGE_IDX(ip);

    // search for existing session
    pthread_rwlock_rdlock(&zinst()->sessions_lock[sidx]);
    HASH_FIND(hh, zinst()->sessions[sidx], &ip, sizeof(ip), sess);
    if (NULL != sess) {
        __atomic_add_fetch(&sess->refcnt, 1, __ATOMIC_RELAXED);
    }
    pthread_rwlock_unlock(&zinst()->sessions_lock[sidx]);

    // or create new session
    if (!existing_only && NULL == sess) {
        pthread_rwlock_wrlock(&zinst()->sessions_lock[sidx]);

        HASH_FIND(hh, zinst()->sessions[sidx], &ip, sizeof(ip), sess);
        if (NULL != sess) {
            __atomic_add_fetch(&sess->refcnt, 1, __ATOMIC_RELAXED);
        } else {
            sess = session_create();
            sess->ip = ip;
            __atomic_store_n(&sess->last_activity, ztime(false), __ATOMIC_RELAXED);
            __atomic_add_fetch(&sess->refcnt, 1, __ATOMIC_RELAXED); // sessions storage reference

            HASH_ADD(hh, zinst()->sessions[sidx], ip, sizeof(ip), sess);
        }

        pthread_rwlock_unlock(&zinst()->sessions_lock[sidx]);
    }

    return sess;
}
Example #2
0
void
test_add ()
{
  v = 0;
  count = 1;

  __atomic_add_fetch (&v, count, __ATOMIC_RELAXED);
  if (v != 1)
    abort ();

  __atomic_fetch_add (&v, count, __ATOMIC_CONSUME);
  if (v != 2)
    abort ();

  __atomic_add_fetch (&v, 1 , __ATOMIC_ACQUIRE);
  if (v != 3)
    abort ();

  __atomic_fetch_add (&v, 1, __ATOMIC_RELEASE);
  if (v != 4)
    abort ();

  __atomic_add_fetch (&v, count, __ATOMIC_ACQ_REL);
  if (v != 5)
    abort ();

  __atomic_fetch_add (&v, count, __ATOMIC_SEQ_CST);
  if (v != 6)
    abort ();
}
Example #3
0
void __hlt_object_ref(const hlt_type_info* ti, void* obj, hlt_execution_context* ctx)
{
    __hlt_gchdr* hdr = (__hlt_gchdr*)obj;
    ;

#ifdef DEBUG
    if ( ! ti->gc ) {
        _dbg_mem_gc("! ref", ti, obj, 0, 0, ctx);
        _internal_memory_error(obj, "__hlt_object_ref", "object not garbage collected", ti);
    }
#endif

#ifdef HLT_ATOMIC_REF_COUNTING
    __atomic_add_fetch(&hdr->ref_cnt, 1, __ATOMIC_SEQ_CST);
#else
    ++hdr->ref_cnt;
#endif

#if 0
    // This is ok now!
    if ( new_ref_cnt <= 0 ) {
        _dbg_mem_gc("! ref", ti, obj, 0, 0, ctx);
        _internal_memory_error(obj, "__hlt_object_ref", "bad reference count", ti);
    }
#endif

#ifdef DEBUG
    ++__hlt_globals()->num_refs;
    _dbg_mem_gc("ref", ti, obj, 0, 0, ctx);
#endif
}
Example #4
0
void Threading::Semaphore::Post(int multiple)
{
	for (int i = 0; i < multiple; ++i) {
		MACH_CHECK(semaphore_signal(m_sema));
	}
	__atomic_add_fetch(&m_counter, multiple, __ATOMIC_SEQ_CST);
}
void hdr_phaser_writer_exit(
    struct hdr_writer_reader_phaser* p, int64_t critical_value_at_enter)
{
    int64_t* end_epoch =
        (critical_value_at_enter < 0) ? &p->odd_end_epoch : &p->even_end_epoch;
    __atomic_add_fetch(end_epoch, 1, __ATOMIC_SEQ_CST);
}
Example #6
0
void* t1(void *param) {
  struct test_params* params = (struct test_params*)param;
  //ac_printf("t1:+params->loops=%d\n", params->loops);
  for(ac_uint i = 0; i < params->loops; i++) {
    __atomic_add_fetch(&params->counter, 1, __ATOMIC_RELEASE);

    //ac_thread_yield();

#ifdef NDEBUG
    // Wait to be signaled
    AcReceptor_wait(params->receptor);
#else
    if ((i % 1) == 0) {
      ac_printf("t1: i=%d flags=%x isr_counter=%d receptor.thdl=%x",
          i, get_flags(), get_timer_reschedule_isr_counter(), params->receptor->thdl);
      print_ready_list(" - ");
    }

    ac_printf("t1: waiting    thdl=%x flags=%x\n", ac_thread_get_cur_hdl(), get_flags());
    ac_u64 wait_start = ac_tscrd();
    AcReceptor_wait(params->receptor);
    ac_u64 ticks = ac_tscrd() - wait_start;
    ac_printf("t1: continuing wait time=%.9t\n", ticks);
#endif
  }

  ac_debug_printf("t1: signal done_receptor\n");
  AcReceptor_signal(params->done_receptor);
  return AC_NULL;
}
Example #7
0
void test_atomic_bool (_Atomic _Bool *a)
{
  enum { SEQ_CST = __ATOMIC_SEQ_CST };
  
  __atomic_fetch_add (a, 1, SEQ_CST);   /* { dg-error "operand type ._Atomic _Bool \\*. is incompatible with argument 1 of .__atomic_fetch_add." } */
  __atomic_fetch_sub (a, 1, SEQ_CST);   /* { dg-error "operand type ._Atomic _Bool \\*. is incompatible with argument 1 of .__atomic_fetch_sub." } */
  __atomic_fetch_and (a, 1, SEQ_CST);   /* { dg-error "operand type ._Atomic _Bool \\*. is incompatible with argument 1 of .__atomic_fetch_and." } */
  __atomic_fetch_xor (a, 1, SEQ_CST);   /* { dg-error "operand type ._Atomic _Bool \\*. is incompatible with argument 1 of .__atomic_fetch_xor." } */
  __atomic_fetch_or (a, 1, SEQ_CST);   /* { dg-error "operand type ._Atomic _Bool \\*. is incompatible with argument 1 of .__atomic_fetch_or." } */
  __atomic_fetch_nand (a, 1, SEQ_CST);   /* { dg-error "operand type ._Atomic _Bool \\*. is incompatible with argument 1 of .__atomic_fetch_nand." } */

  __atomic_add_fetch (a, 1, SEQ_CST);   /* { dg-error "operand type ._Atomic _Bool \\*. is incompatible with argument 1 of .__atomic_add_fetch." } */
  __atomic_sub_fetch (a, 1, SEQ_CST);   /* { dg-error "operand type ._Atomic _Bool \\*. is incompatible with argument 1 of .__atomic_sub_fetch." } */
  __atomic_and_fetch (a, 1, SEQ_CST);   /* { dg-error "operand type ._Atomic _Bool \\*. is incompatible with argument 1 of .__atomic_and_fetch." } */
  __atomic_xor_fetch (a, 1, SEQ_CST);   /* { dg-error "operand type ._Atomic _Bool \\*. is incompatible with argument 1 of .__atomic_xor_fetch." } */
  __atomic_or_fetch (a, 1, SEQ_CST);   /* { dg-error "operand type ._Atomic _Bool \\*. is incompatible with argument 1 of .__atomic_or_fetch." } */
  __atomic_nand_fetch (a, 1, SEQ_CST);   /* { dg-error "operand type ._Atomic _Bool \\*. is incompatible with argument 1 of .__atomic_nand_fetch." } */

  /* The following are valid and must be accepted.  */
  _Bool val = 0, ret = 0;
  __atomic_exchange (a, &val, &ret, SEQ_CST);
  __atomic_exchange_n (a, val, SEQ_CST);
  __atomic_compare_exchange (a, &val, &ret, !1, SEQ_CST, SEQ_CST);
  __atomic_compare_exchange_n (a, &val, ret, !1, SEQ_CST, SEQ_CST);
  __atomic_test_and_set (a, SEQ_CST);
  __atomic_clear (a, SEQ_CST);
}
Example #8
0
inline T Atomic<T>::addAndFetch ( const T& val )
{
#ifdef HAVE_NEW_GCC_ATOMIC_OPS
   return __atomic_add_fetch(&_value, val, __ATOMIC_ACQ_REL);
#else
   return __sync_add_and_fetch( &_value,val );
#endif
}
Example #9
0
/**
 * Create new empty session.
 * @return New session pointer.
 */
struct zsession *session_create()
{
    struct zsession *sess = malloc(sizeof(*sess));

    bzero(sess, sizeof(*sess));
    sess->client = client_create();
    client_session_add(sess->client, sess);

    // set default values
    sess->refcnt = 1; // caller references this entry
    pthread_spin_init(&sess->_nat_lock, PTHREAD_PROCESS_PRIVATE);
    pthread_rwlock_init(&sess->lock_client, NULL);

    __atomic_add_fetch(&zinst()->sessions_cnt, 1, __ATOMIC_RELAXED);
    __atomic_add_fetch(&zinst()->unauth_sessions_cnt, 1, __ATOMIC_RELAXED);

    return sess;
}
Example #10
0
/**
 * Handle the one_shot interrupt.
 *
 * NOTE: Interrupts are enabled so __atomic operations are used.
 */
void periodic_handler(ac_uptr param) {
  irq_param* pirq_param = (irq_param*)param;
  ac_bool ac_true = AC_TRUE;
  ac_bool* psource = &pirq_param->source;

  ac_bool ok = __atomic_compare_exchange_n(psource, &ac_true, AC_FALSE,
      AC_TRUE, __ATOMIC_RELEASE, __ATOMIC_ACQUIRE);
  if (ok) {
    __atomic_add_fetch(&periodic_counter, 1, __ATOMIC_RELEASE);
    ac_debug_printf("periodic: %d inc counter\n\n", pirq_param->timer);
  }
}
Example #11
0
static void radeon_update_memory_usage(struct radeon_bo *bo,
				       unsigned mem_type, int sign)
{
	struct radeon_device *rdev = bo->rdev;
	u64 size = (u64)bo->tbo.num_pages << PAGE_SHIFT;

	switch (mem_type) {
	case TTM_PL_TT:
		if (sign > 0)
			__atomic_add_fetch(&rdev->gtt_usage.counter, size,__ATOMIC_RELAXED);
		else
			__atomic_sub_fetch(&rdev->gtt_usage.counter, size,__ATOMIC_RELAXED);
		break;
	case TTM_PL_VRAM:
		if (sign > 0)
			__atomic_add_fetch(&rdev->vram_usage.counter, size,__ATOMIC_RELAXED);
		else
			__atomic_sub_fetch(&rdev->vram_usage.counter, size,__ATOMIC_RELAXED );
		break;
	}
}
Example #12
0
_MCFCRT_TlsKeyHandle _MCFCRT_TlsAllocKey(size_t uSize, _MCFCRT_TlsConstructor pfnConstructor, _MCFCRT_TlsDestructor pfnDestructor, intptr_t nContext){
	static volatile size_t s_uKeyCounter;

	TlsKey *const pKey = _MCFCRT_malloc(sizeof(TlsKey));
	if(!pKey){
		return _MCFCRT_NULLPTR;
	}
	pKey->uCounter       = __atomic_add_fetch(&s_uKeyCounter, 1, __ATOMIC_RELAXED);
	pKey->uSize          = uSize;
	pKey->pfnConstructor = pfnConstructor;
	pKey->pfnDestructor  = pfnDestructor;
	pKey->nContext       = nContext;

	return (_MCFCRT_TlsKeyHandle)pKey;
}
Example #13
0
void* t1(void *param) {
  struct test_params* params = (struct test_params*)param;
  //ac_printf("t1:+params->loops=%d\n", params->loops);
  for(ac_uint i = 0; i < params->loops; i++) {
    __atomic_add_fetch(&params->counter, 1, __ATOMIC_RELEASE);

    //ac_thread_yield();

    AcReceptor_wait(params->receptor);
  }

  ac_debug_printf("t1: signal done_receptor\n");
  AcReceptor_signal(params->done_receptor);
  return AC_NULL;
}
Example #14
0
size_t zmalloc_used_memory(void) {
    size_t um;

    if (zmalloc_thread_safe) {
#ifdef HAVE_ATOMIC
        um = __atomic_add_fetch(&used_memory, 0, __ATOMIC_SEQ_CST);
#else
        pthread_mutex_lock(&used_memory_mutex);
        um = used_memory;
        pthread_mutex_unlock(&used_memory_mutex);
#endif
    }
    else {
        um = used_memory;
    }

    return um;
}
Example #15
0
int
main ()
{

  ac = __atomic_exchange_n (&bc, cc, __ATOMIC_RELAXED);
  if (bc != 1)
    abort ();

  as = __atomic_load_n (&bs, __ATOMIC_SEQ_CST);
  if (bs != 1)
    abort ();

  __atomic_store_n (&ac, bc, __ATOMIC_RELAXED);
  if (ac != 1)
    abort ();

  __atomic_compare_exchange_n (&as, &bs, cs, 0, __ATOMIC_SEQ_CST, __ATOMIC_ACQUIRE);
  if (as != 1)
    abort ();

  ac = __atomic_fetch_add (&cc, 15, __ATOMIC_SEQ_CST);
  if (cc != 1)
    abort ();

  /* This should be translated to __atomic_fetch_add for the library */
  as = __atomic_add_fetch (&cs, 10, __ATOMIC_RELAXED);

  if (cs != 1)
    abort ();

  /* The fake external function should return 10.  */
  if (__atomic_is_lock_free (4, 0) != 10)
    abort ();
   
  /* PR 51040 was caused by arithmetic code not patching up nand_fetch properly
     when used an an external function.  Look for proper return value here.  */
  ac = 0x3C;
  bc = __atomic_nand_fetch (&ac, 0x0f, __ATOMIC_RELAXED);
  if (bc != ac)
    abort ();

  return 0;
}
Example #16
0
static void *
creator_thread (void *arg)
{
  int ret;
  xpthread_barrier_wait (&barrier);

  while (true)
    {
      pthread_t thr;
      /* Thread creation will fail if the kernel does not free old
	 threads quickly enough, so we do not report errors.  */
      ret = pthread_create (&thr, &detached, do_nothing, NULL);
      if (ret == 0 && __atomic_add_fetch (&threads_created, 1, __ATOMIC_SEQ_CST)
          >= threads_to_create)
        break;
    }

  return NULL;
}
Example #17
0
int CRYPTO_atomic_add(int *val, int amount, int *ret, CRYPTO_RWLOCK *lock)
{
# if defined(__GNUC__) && defined(__ATOMIC_ACQ_REL)
    if (__atomic_is_lock_free(sizeof(*val), val)) {
        *ret = __atomic_add_fetch(val, amount, __ATOMIC_ACQ_REL);
        return 1;
    }
# endif
    if (!CRYPTO_THREAD_write_lock(lock))
        return 0;

    *val += amount;
    *ret  = *val;

    if (!CRYPTO_THREAD_unlock(lock))
        return 0;

    return 1;
}
        void connection::make_sequence() {
            do {
            // ID主要用于判定超时的时候,防止地址或名称重用,超时时间内64位整数不可能会重复
#if defined(HIREDIS_HAPP_ATOMIC_STD)
            static std::atomic<uint64_t> seq_alloc(0);
            sequence = seq_alloc.fetch_add(1);

#elif defined(HIREDIS_HAPP_ATOMIC_MSVC)
            static LONGLONG volatile seq_alloc = 0;
            sequence = static_cast<uint64_t>(InterlockedAdd64(&seq_alloc, 1));

#elif defined(HIREDIS_HAPP_ATOMIC_GCC_ATOMIC)
            static volatile uint64_t seq_alloc = 0;
            sequence = __atomic_add_fetch(&seq_alloc, 1, __ATOMIC_SEQ_CST);
#elif defined(HIREDIS_HAPP_ATOMIC_GCC)
            static volatile uint64_t seq_alloc = 0;
            sequence = __sync_fetch_and_add (&seq_alloc, 1);
#else
            static volatile uint64_t seq_alloc = 0;
            sequence = ++ seq_alloc;
#endif
            } while(0 == sequence);
        }
Example #19
0
void SyslogInput_JSON( char *syslog_string, struct _SyslogInput *SyslogInput )
{

    struct json_object *json_obj = NULL;
    struct json_object *tmp = NULL;

    struct json_object_iterator it;
    struct json_object_iterator itEnd;

    const char *val_str = NULL;

    uint16_t json_str_count=1;
    uint16_t a;

    bool has_message = false;

    char json_str[JSON_MAX_NEST][JSON_MAX_SIZE] = { { 0 } };

    memset(SyslogInput, 0, sizeof(_SyslogInput));

    memcpy(SyslogInput->syslog_message, "UNDEFINED\0", sizeof(SyslogInput->syslog_message));
    memcpy(SyslogInput->syslog_program, "UNDEFINED\0", sizeof(SyslogInput->syslog_program));
    memcpy(SyslogInput->syslog_time, "UNDEFINED\0", sizeof(SyslogInput->syslog_time));
    memcpy(SyslogInput->syslog_date, "UNDEFINED\0", sizeof(SyslogInput->syslog_date));
    memcpy(SyslogInput->syslog_tag, "UNDEFINED\0", sizeof(SyslogInput->syslog_tag));
    memcpy(SyslogInput->syslog_level, "UNDEFINED\0", sizeof(SyslogInput->syslog_level));
    memcpy(SyslogInput->syslog_priority, "UNDEFINED\0", sizeof(SyslogInput->syslog_priority));
    memcpy(SyslogInput->syslog_facility, "UNDEFINED\0", sizeof(SyslogInput->syslog_facility));
    memcpy(SyslogInput->syslog_host, "UNDEFINED\0", sizeof(SyslogInput->syslog_host));

    /* If the json isn't nested,  we can do this the easy way */

    if ( Syslog_JSON_Map->is_nested == false )
        {

            json_obj = json_tokener_parse(syslog_string);

            if ( json_obj == NULL )
                {

                    if ( syslog_string != NULL )
                        {
                            Sagan_Log(WARN, "[%s, line %d] Libfastjson failed to decode JSON. Get: %s", __FILE__, __LINE__, syslog_string);
                        }
                    else
                        {
                            Sagan_Log(WARN, "[%s, line %d] Libfastjson failed to decode JSON. Got NULL data.", __FILE__, __LINE__);
                        }


                    json_object_put(json_obj);
                    __atomic_add_fetch(&counters->malformed_json_input_count, 1, __ATOMIC_SEQ_CST);
                    return;
                }


            __atomic_add_fetch(&counters->json_input_count, 1, __ATOMIC_SEQ_CST);

            if ( json_object_object_get_ex(json_obj, Syslog_JSON_Map->syslog_map_host, &tmp))
                {
                    strlcpy(SyslogInput->syslog_host, json_object_get_string(tmp), sizeof(SyslogInput->syslog_host));
                }

            if ( json_object_object_get_ex(json_obj, Syslog_JSON_Map->syslog_map_facility, &tmp))
                {
                    strlcpy(SyslogInput->syslog_facility, json_object_get_string(tmp), sizeof(SyslogInput->syslog_facility));
                }

            if ( json_object_object_get_ex(json_obj, Syslog_JSON_Map->syslog_map_priority, &tmp))
                {
                    strlcpy(SyslogInput->syslog_priority, json_object_get_string(tmp), sizeof(SyslogInput->syslog_priority));
                }

            if ( json_object_object_get_ex(json_obj, Syslog_JSON_Map->syslog_map_level, &tmp))
                {
                    strlcpy(SyslogInput->syslog_level, json_object_get_string(tmp), sizeof(SyslogInput->syslog_level));
                }

            if ( json_object_object_get_ex(json_obj, Syslog_JSON_Map->syslog_map_tag, &tmp))
                {
                    strlcpy(SyslogInput->syslog_tag, json_object_get_string(tmp), sizeof(SyslogInput->syslog_tag));
                }

            if ( json_object_object_get_ex(json_obj, Syslog_JSON_Map->syslog_map_date, &tmp))
                {
                    strlcpy(SyslogInput->syslog_date, json_object_get_string(tmp), sizeof(SyslogInput->syslog_date));
                }

            if ( json_object_object_get_ex(json_obj, Syslog_JSON_Map->syslog_map_time, &tmp))
                {
                    strlcpy(SyslogInput->syslog_time, json_object_get_string(tmp), sizeof(SyslogInput->syslog_time));
                }

            if ( json_object_object_get_ex(json_obj, Syslog_JSON_Map->syslog_map_program, &tmp))
                {
                    strlcpy(SyslogInput->syslog_program, json_object_get_string(tmp), sizeof(SyslogInput->syslog_program));
                }

            if ( json_object_object_get_ex(json_obj, Syslog_JSON_Map->syslog_map_message, &tmp))
                {
                    snprintf(SyslogInput->syslog_message, sizeof(SyslogInput->syslog_message)," %s", json_object_get_string(tmp));
                    SyslogInput->syslog_message[ (sizeof(SyslogInput->syslog_message) -1 ) ] = '\0';
                    has_message = true;
                }

        }
    else
        {

            /* The raw syslog is the first "nested" level".  Copy that */

            strlcpy(json_str[0], syslog_string, sizeof(json_str[0]));
            json_obj = json_tokener_parse(syslog_string);

            if ( json_obj == NULL )
                {

                    if ( debug->debugmalformed )
                        {
                            Sagan_Log(WARN, "[%s, line %d] Libfastjson failed to decode JSON input. The log line was: \"%s\"", __FILE__, __LINE__, syslog_string);
                        }

                    json_object_put(json_obj);
                    __atomic_add_fetch(&counters->malformed_json_input_count, 1, __ATOMIC_SEQ_CST);
                    return;
                }

            it = json_object_iter_begin(json_obj);
            itEnd = json_object_iter_end(json_obj);

            /* Search through all key/values looking for embedded JSON */

            while (!json_object_iter_equal(&it, &itEnd))
                {

                    struct json_object *const val = json_object_iter_peek_value(&it);
                    val_str = json_object_get_string(val);

                    if ( val_str[0] == '{' || val_str[1] == '{' )
                        {

                            /* If object looks like JSON, add it to array to be parsed later */

                            if ( json_str_count < JSON_MAX_NEST )
                                {
                                    strlcpy(json_str[json_str_count], val_str, sizeof(json_str[json_str_count]));
                                    json_str_count++;
                                }
                            else
                                {
                                    Sagan_Log(ERROR, "[%s, line %d] Detected JSON past max nest of %d! Skipping extra JSON.", __FILE__, __LINE__, JSON_MAX_NEST);
                                }
                        }

                    json_object_iter_next(&it);

                    /* Search through the nest to see if we can find out values */

                    for ( a = 0; a < json_str_count; a++ )
                        {

                            struct json_object *json_obj = NULL;
                            json_obj = json_tokener_parse(json_str[a]);

                            if ( json_obj == NULL )
                                {
                                    Sagan_Log(WARN, "[%s, line %d] Detected JSON nest but Libfastjson errors. The log line was: \"%s\"", __FILE__, __LINE__, json_str[a]);
                                    json_object_put(json_obj);
                                    __atomic_add_fetch(&counters->malformed_json_input_count, 1, __ATOMIC_SEQ_CST);
                                    return;
                                }

                            __atomic_add_fetch(&counters->json_input_count, 1, __ATOMIC_SEQ_CST);

                            if ( json_object_object_get_ex(json_obj, Syslog_JSON_Map->syslog_map_message, &tmp))
                                {
                                    snprintf(SyslogInput->syslog_message, sizeof(SyslogInput->syslog_message)," %s", json_object_get_string(tmp));
                                    SyslogInput->syslog_message[ (sizeof(SyslogInput->syslog_message) -1 ) ] = '\0';
                                    has_message = true;
                                }

                            if ( json_object_object_get_ex(json_obj, Syslog_JSON_Map->syslog_map_host, &tmp))
                                {
                                    strlcpy(SyslogInput->syslog_host, json_object_get_string(tmp), sizeof(SyslogInput->syslog_host));
                                }

                            if ( json_object_object_get_ex(json_obj, Syslog_JSON_Map->syslog_map_facility, &tmp))
                                {
                                    strlcpy(SyslogInput->syslog_facility, json_object_get_string(tmp), sizeof(SyslogInput->syslog_facility));
                                }

                            if ( json_object_object_get_ex(json_obj, Syslog_JSON_Map->syslog_map_priority, &tmp))
                                {
                                    strlcpy(SyslogInput->syslog_priority, json_object_get_string(tmp), sizeof(SyslogInput->syslog_priority));
                                }

                            if ( json_object_object_get_ex(json_obj, Syslog_JSON_Map->syslog_map_level, &tmp))
                                {
                                    strlcpy(SyslogInput->syslog_level, json_object_get_string(tmp), sizeof(SyslogInput->syslog_level));
                                }

                            if ( json_object_object_get_ex(json_obj, Syslog_JSON_Map->syslog_map_tag, &tmp))
                                {
                                    strlcpy(SyslogInput->syslog_tag, json_object_get_string(tmp), sizeof(SyslogInput->syslog_tag));
                                }

                            if ( json_object_object_get_ex(json_obj, Syslog_JSON_Map->syslog_map_date, &tmp))
                                {
                                    strlcpy(SyslogInput->syslog_date, json_object_get_string(tmp), sizeof(SyslogInput->syslog_date));
                                }

                            if ( json_object_object_get_ex(json_obj, Syslog_JSON_Map->syslog_map_time, &tmp))
                                {
                                    strlcpy(SyslogInput->syslog_time, json_object_get_string(tmp), sizeof(SyslogInput->syslog_time));
                                }

                            if ( json_object_object_get_ex(json_obj, Syslog_JSON_Map->syslog_map_program, &tmp))
                                {
                                    strlcpy(SyslogInput->syslog_program, json_object_get_string(tmp), sizeof(SyslogInput->syslog_program));
                                }

                        }

                }
        }


    if ( has_message == false )
        {
            Sagan_Log(WARN, "[%s, line %d] Received JSON which has no decoded 'message' value. The log line was: \"%s\"", __FILE__, __LINE__, syslog_string);
        }

    json_object_put(json_obj);
}
Example #20
0
int
atomic_add_fetch_ACQUIRE ()
{
  return __atomic_add_fetch (&v, 4096, __ATOMIC_ACQUIRE);
}
Example #21
0
void Load_Gen_Map( const char *genmap )
{

    FILE *genmapfile;
    char genbuf[1024];

    char *saveptr=NULL;

    char *gen1=NULL;
    char *gen2=NULL;
    char *gen3=NULL;

    Sagan_Log(NORMAL, "Loading gen-msg.map file. [%s]", genmap);

    __atomic_store_n (&counters->genmapcount, 0, __ATOMIC_SEQ_CST);

    if (( genmapfile = fopen(genmap, "r" )) == NULL )
        {
            Sagan_Log(ERROR, "[%s, line %d] Cannot open generator file (%s)", __FILE__, __LINE__, genmap);
        }

    while(fgets(genbuf, 1024, genmapfile) != NULL)
        {

            /* Skip comments and blank linkes */

            if (genbuf[0] == '#' || genbuf[0] == 10 || genbuf[0] == ';' || genbuf[0] == 32)
                {
                    continue;
                }

            /* Allocate memory for references,  not comments */

            generator = (_Sagan_Processor_Generator *) realloc(generator, (counters->genmapcount+1) * sizeof(_Sagan_Processor_Generator));

            if ( generator == NULL )
                {
                    Sagan_Log(ERROR, "[%s, line %d] Failed to reallocate memory for generator. Abort!", __FILE__, __LINE__);
                }

            memset(&generator[counters->genmapcount], 0, sizeof(_Sagan_Processor_Generator));

            gen1 = strtok_r(genbuf, "|", &saveptr);

            if ( gen1 == NULL )
                {
                    Sagan_Log(ERROR, "%s is incorrect or not correctly formated (gen1) ", genmap);
                }

            Remove_Return(gen1);

            gen2 = strtok_r(NULL, "|", &saveptr);

            if ( gen2 == NULL )
                {
                    Sagan_Log(ERROR, "%s is incorrect or not correctly formated (gen2) ", genmap);
                }

            Remove_Return(gen2);

            gen3 = strtok_r(NULL, "|", &saveptr);

            if ( gen3 == NULL )
                {
                    Sagan_Log(ERROR, "%s is incorrect or not correctly formated (gen3) ", genmap);
                }

            Remove_Return(gen3);

            generator[counters->genmapcount].generatorid=atoi(gen1);
            generator[counters->genmapcount].alertid=atoi(gen2);
            strlcpy(generator[counters->genmapcount].generator_msg, gen3, sizeof(generator[counters->genmapcount].generator_msg));

            __atomic_add_fetch(&counters->genmapcount, 1, __ATOMIC_SEQ_CST);

        }

    fclose(genmapfile);
    Sagan_Log(NORMAL, "%d generators loaded.", counters->genmapcount);
}
int64_t hdr_phaser_writer_enter(struct hdr_writer_reader_phaser* p)
{
    return __atomic_add_fetch(&p->start_epoch, 1, __ATOMIC_SEQ_CST);
}
Example #23
0
void Threading::Semaphore::Post()
{
	MACH_CHECK(semaphore_signal(m_sema));
	__atomic_add_fetch(&m_counter, 1, __ATOMIC_SEQ_CST);
}
Example #24
0
proc_t* create_init_process_structure(uintptr_t pml) {
    proc_t* process = malloc(sizeof(proc_t));
    if (process == NULL) {
        error(ERROR_MINIMAL_MEMORY_FAILURE, 0, 0, &create_init_process_structure);
    }
    memset(process, 0, sizeof(proc_t));

    proc_spinlock_lock(&__proclist_lock);
    process->proc_id = ++process_id_num;
    list_push_right(processes, process);
    proc_spinlock_unlock(&__proclist_lock);

    process->fds = create_array();
    process->pprocess = true;
    if (process->fds == NULL) {
        error(ERROR_MINIMAL_MEMORY_FAILURE, 0, 0, &create_init_process_structure);
    }
    process->threads = create_array();
    if (process->threads == NULL) {
        error(ERROR_MINIMAL_MEMORY_FAILURE, 0, 0, &create_init_process_structure);
    }
    process->pml4 = pml;
    process->mem_maps = NULL;
    process->proc_random = rg_create_random_generator(get_unix_time());
    process->parent = NULL;
    process->priority = 0;
    process->process_list.data = process;
    process->futexes = create_uint64_table();
    process->__ob_lock = 0;
    if (process->futexes == NULL) {
        error(ERROR_MINIMAL_MEMORY_FAILURE, 0, 0, &create_init_process_structure);
    }

    process->input_buffer = create_queue_static(__message_getter);
    if (process->input_buffer == NULL) {
        error(ERROR_MINIMAL_MEMORY_FAILURE, 0, 0, &create_init_process_structure);
    }

    process->pq_input_buffer = create_queue_static(__message_getter);
    if (process->pq_input_buffer == NULL) {
        error(ERROR_MINIMAL_MEMORY_FAILURE, 0, 0, &create_init_process_structure);
    }

    process->blocked_wait_messages = create_list_static(__message_getter);
    if (process->blocked_wait_messages == NULL) {
        error(ERROR_MINIMAL_MEMORY_FAILURE, 0, 0, &create_init_process_structure);
    }

    process->temp_processes = create_list_static(__process_get_function);
	if (process->blocked_wait_messages == NULL) {
		error(ERROR_MINIMAL_MEMORY_FAILURE, 0, 0, &create_init_process_structure);
	}

    thread_t* main_thread = malloc(sizeof(thread_t));
    if (main_thread == NULL) {
        error(ERROR_MINIMAL_MEMORY_FAILURE, 0, 0, &create_init_process_structure);
    }
    memset(main_thread, 0, sizeof(thread_t));
    main_thread->parent_process = process;
    main_thread->tId = __atomic_add_fetch(&thread_id_num, 1, __ATOMIC_SEQ_CST);
    main_thread->priority = 0;
    main_thread->futex_block = create_list_static(__blocked_getter);
    if (main_thread->futex_block == NULL) {
        error(ERROR_MINIMAL_MEMORY_FAILURE, 0, 0, &create_init_process_structure);
    }
    main_thread->blocked = false;
    main_thread->blocked_list.data = main_thread;
    main_thread->schedule_list.data = main_thread;
    main_thread->last_rdi = (ruint_t)(uintptr_t)process->argc;
    main_thread->last_rsi = (ruint_t)(uintptr_t)process->argv;
    main_thread->last_rdx = (ruint_t)(uintptr_t)process->environ;

    main_thread->continuation = malloc(sizeof(continuation_t));
    if (main_thread->continuation == NULL) {
        error(ERROR_MINIMAL_MEMORY_FAILURE, 0, 0, &create_init_process_structure);
    }
    main_thread->continuation->present = false;

    if (array_push_data(process->threads, main_thread) == 0) {
        error(ERROR_MINIMAL_MEMORY_FAILURE, 0, 0, &create_init_process_structure);
    }

    return process;
}
Example #25
0
int create_process_base(uint8_t* image_data, int argc, char** argv,
        char** envp, proc_t** cpt, uint8_t asked_priority, registers_t* r) {
    proc_spinlock_lock(&__thread_modifier);
    uint8_t cpp = get_current_cput()->ct->parent_process->priority;
    proc_spinlock_unlock(&__thread_modifier);

    if (cpp > asked_priority) {
        return EINVAL;
    }

    int envc = 0;
    char** envt = envp;
    while (*envt != NULL) {
        ++envc;
        ++envt;
    }

    int err;
    if ((err = cpy_array(argc, &argv)) != 0)
        return err;
    if ((err = cpy_array(envc, &envp)) != 0) {
        free_array(argc, argv);
        return err;
    }
    // envp and argv are now kernel structures

    proc_t* process = malloc(sizeof(proc_t));
    if (process == NULL) {
        return ENOMEM_INTERNAL;
    }
    memset(process, 0, sizeof(proc_t));

    process->__ob_lock = 0;
    process->process_list.data = process;
    process->pprocess = true;

    process->fds = create_array();
    if (process->fds == NULL) {
        free(process);
        return ENOMEM_INTERNAL;
    }

    process->threads = create_array();
    if (process->fds == NULL) {
        destroy_array(process->fds);
        free(process);
        return ENOMEM_INTERNAL;
    }

    process->mem_maps = NULL;
    process->proc_random = rg_create_random_generator(get_unix_time());
    process->parent = NULL;
    process->priority = asked_priority;
    process->pml4 = create_pml4();
    if (process->pml4 == 0) {
        destroy_array(process->fds);
        free(process);
        return ENOMEM_INTERNAL;
    }

    process->futexes = create_uint64_table();

    if (process->futexes == NULL) {
        destroy_array(process->threads);
        destroy_array(process->fds);
        free(process);
        return ENOMEM_INTERNAL;
    }

    process->input_buffer = create_queue_static(__message_getter);

    if (process->input_buffer == NULL) {
        destroy_table(process->futexes);
        destroy_array(process->threads);
        destroy_array(process->fds);
        free(process);
        return ENOMEM_INTERNAL;
    }

    process->pq_input_buffer = create_queue_static(__message_getter);

    if (process->input_buffer == NULL) {
        free_queue(process->input_buffer);
        destroy_table(process->futexes);
        destroy_array(process->threads);
        destroy_array(process->fds);
        free(process);
        return ENOMEM_INTERNAL;
    }

    process->blocked_wait_messages = create_list_static(__message_getter);
    if (process->blocked_wait_messages == NULL) {
        free_queue(process->pq_input_buffer);
        free_queue(process->input_buffer);
        destroy_table(process->futexes);
        destroy_array(process->threads);
        destroy_array(process->fds);
        free(process);
        return ENOMEM_INTERNAL;
    }

    process->temp_processes = create_list_static(__process_get_function);
	if (process->blocked_wait_messages == NULL) {
		free_list(process->blocked_wait_messages);
		free_queue(process->pq_input_buffer);
		free_queue(process->input_buffer);
		destroy_table(process->futexes);
		destroy_array(process->threads);
		destroy_array(process->fds);
		free(process);
		return ENOMEM_INTERNAL;
	}

    thread_t* main_thread = malloc(sizeof(thread_t));
    if (main_thread == NULL) {
    	free_list(process->temp_processes);
        free_list(process->blocked_wait_messages);
        free_queue(process->pq_input_buffer);
        free_queue(process->input_buffer);
        destroy_table(process->futexes);
        destroy_array(process->threads);
        destroy_array(process->fds);
        free(process);
        // TODO: free process address page
        return ENOMEM_INTERNAL;
    }
    memset(main_thread, 0, sizeof(thread_t));
    main_thread->parent_process = process;
    main_thread->priority = asked_priority;
    main_thread->blocked = false;

    main_thread->continuation = malloc(sizeof(continuation_t));
    if (main_thread->continuation == NULL) {
        free(main_thread);
        free_list(process->temp_processes);
        free_list(process->blocked_wait_messages);
        free_queue(process->pq_input_buffer);
        free_queue(process->input_buffer);
        destroy_table(process->futexes);
        destroy_array(process->threads);
        destroy_array(process->fds);
        free(process);
        // TODO: free process address page
        return ENOMEM_INTERNAL;
    }
    main_thread->continuation->present = false;

    main_thread->futex_block = create_list_static(__blocked_getter);
    if (main_thread->futex_block == NULL) {
        free(main_thread->continuation);
        free(main_thread);
        free_list(process->temp_processes);
        free_list(process->blocked_wait_messages);
        free_queue(process->pq_input_buffer);
        free_queue(process->input_buffer);
        destroy_table(process->futexes);
        destroy_array(process->threads);
        destroy_array(process->fds);
        free(process);
        // TODO: free process address page
        return ENOMEM_INTERNAL;
    }

    array_push_data(process->threads, main_thread);

    err = load_elf_exec((uintptr_t)image_data, process);
    if (err == ELF_ERROR_ENOMEM) {
        err = ENOMEM_INTERNAL;
    } else if (err != 0) {
        err = EINVAL;
    }

    if (err != 0) {
        free_list(main_thread->futex_block);
        free(main_thread->continuation);
        free(main_thread);
        free_list(process->temp_processes);
        free_list(process->blocked_wait_messages);
        free_queue(process->pq_input_buffer);
        free_queue(process->input_buffer);
        destroy_table(process->futexes);
        destroy_array(process->threads);
        destroy_array(process->fds);
        free(process);
        // TODO: free process address page
        return err;
    }

    char** argvu = argv;
    char** envpu = envp;
    if ((err = cpy_array_user(argc, &argvu, process)) != 0) {
        free_list(main_thread->futex_block);
        free(main_thread->continuation);
        free(main_thread);
        free_list(process->temp_processes);
        free_list(process->blocked_wait_messages);
        free_queue(process->pq_input_buffer);
        free_queue(process->input_buffer);
        destroy_table(process->futexes);
        destroy_array(process->threads);
        destroy_array(process->fds);
        free(process);
        // TODO: free process address page
        return err;
    }
    if ((err = cpy_array_user(envc, &envpu, process)) != 0) {
        free_list(main_thread->futex_block);
        free(main_thread->continuation);
        free(main_thread);
        free_list(process->temp_processes);
        free_list(process->blocked_wait_messages);
        free_queue(process->pq_input_buffer);
        free_queue(process->input_buffer);
        destroy_table(process->futexes);
        destroy_array(process->threads);
        destroy_array(process->fds);
        free(process);
        // TODO: free process address page
        return err;
    }

    main_thread->local_info = proc_alloc_direct(process, sizeof(tli_t));
    if (main_thread->local_info == NULL) {
        free_list(main_thread->futex_block);
        free(main_thread->continuation);
        free(main_thread);
        free_list(process->temp_processes);
        free_list(process->blocked_wait_messages);
        free_queue(process->pq_input_buffer);
        free_queue(process->input_buffer);
        destroy_table(process->futexes);
        destroy_array(process->threads);
        destroy_array(process->fds);
        free(process);
        // TODO: free process address page
        return ENOMEM_INTERNAL;
    }
    tli_t* li = different_page_mem(process->pml4, main_thread->local_info);
    li->self = main_thread->local_info;

    for (int i=0; i<MESSAGE_BUFFER_CNT; i++) {
        _message_t* m = &process->output_buffer[i];
        m->owner = process;
        m->used = false;
        m->message = proc_alloc_direct(process, 0x200000);
        if (m->message == NULL) {
            free_list(main_thread->futex_block);
            free(main_thread->continuation);
            free(main_thread);
            free_list(process->temp_processes);
            free_list(process->blocked_wait_messages);
            free_queue(process->pq_input_buffer);
            free_queue(process->input_buffer);
            destroy_table(process->futexes);
            destroy_array(process->threads);
            destroy_array(process->fds);
            free(process);
            // TODO: free process address page
            return ENOMEM_INTERNAL;
        }
    }

    process->argc = argc;
    process->argv = argvu;
    process->environ = envpu;

    main_thread->last_r12 = 0;
    main_thread->last_r11 = 0;
    main_thread->last_r10 = 0;
    main_thread->last_r9 = 0;
    main_thread->last_r8 = 0;
    main_thread->last_rax = 0;
    main_thread->last_rbx = 0;
    main_thread->last_rcx = 0;
    main_thread->last_rdx = 0;
    main_thread->last_rdi = 0;
    main_thread->last_rsi = 0;
    main_thread->last_rbp = 0;
    main_thread->last_rflags = 0x200; // enable interrupts

    proc_spinlock_lock(&__proclist_lock);
    main_thread->tId = __atomic_add_fetch(&thread_id_num, 1, __ATOMIC_SEQ_CST);
    process->proc_id = ++process_id_num;
    list_push_right(processes, process);
    proc_spinlock_unlock(&__proclist_lock);

    li->t = main_thread->tId;
    main_thread->last_rdi = (ruint_t)(uintptr_t)process->argc;
    main_thread->last_rsi = (ruint_t)(uintptr_t)process->argv;
    main_thread->last_rdx = (ruint_t)(uintptr_t)process->environ;
    main_thread->blocked_list.data = main_thread;
    main_thread->schedule_list.data = main_thread;

    free_array(argc, argv);
    free_array(envc, envp);

    // TODO: add split option?
    main_thread->last_rax = process->proc_id;
    enschedule_best(main_thread);
    *cpt = process;
    return 0;
}
void unlock(simple_futex_t *futex) {
    int current = __atomic_add_fetch(&futex->turn, 1, __ATOMIC_RELEASE);
    if (futex->number >= current) {
        syscall(__NR_futex, &futex->turn, FUTEX_WAKE, INT_MAX, NULL, 0, 0);
    }
}
Example #27
0
/**
 * Thread scheduler for timer_reschedule_isr.
 * For internal only use only and ASSUMES
 * interrupts are DISABLED!
 *
 * @param sp is the stack of the current thread
 * @param ss is the stack segment of the current thread
 *
 * @return the tcb of the next thread to run
 */
tcb_x86* timer_thread_scheduler_intr_disabled(ac_u8* sp, ac_u16 ss) {
  tcb_x86* ptcb = thread_scheduler_intr_disabled(AC_FALSE, (ac_u8*)sp, ss);
  __atomic_add_fetch(&timer_reschedule_isr_counter, 1, __ATOMIC_RELEASE);
  send_apic_eoi();
  return ptcb;
}