当前位置: 首页>>代码示例>>C++>>正文


C++ spinlock类代码示例

本文整理汇总了C++中spinlock的典型用法代码示例。如果您正苦于以下问题:C++ spinlock类的具体用法?C++ spinlock怎么用?C++ spinlock使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了spinlock类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: notify_all

    void notify_all() const
    {
        spinlock_.lock();

        broadcasting_ = waiters_ > 0;

        if ( broadcasting_ )
        {
            ZI_VERIFY( win32::ReleaseSemaphore( semaphore_, waiters_, 0 ) );
            spinlock_.unlock();
            ZI_VERIFY_0( win32::WaitForSingleObject( last_event_, win32::forever ) );
            broadcasting_ = 0;
        }
        else
        {
            spinlock_.unlock();
        }
    }
开发者ID:AlbertYuChen,项目名称:block_down_sample_,代码行数:18,代码来源:condition_variable.hpp

示例2: scoped_lock

 explicit scoped_lock( spinlock & sp ): sp_( sp )
 {
     sp.lock();
 }
开发者ID:AmesianX,项目名称:memz,代码行数:4,代码来源:spinlock_nt.hpp

示例3: suspend

void OSThread::suspend(spinlock& lock)
{
    lock.unlock();
    suspend();
}
开发者ID:xicilion,项目名称:fibjs_vender,代码行数:5,代码来源:thread.cpp

示例4: profiler_thread

/**
 * Body of the main profiler thread
 */
void profiler::profiler_thread(spinlock& l) {
  // Open the output file
  ofstream output;
  output.open(_output_filename, ios_base::app);
  output.rdbuf()->pubsetbuf(0, 0);
  output.setf(ios::fixed, ios::floatfield);
  output.precision(2);

  // Initialize the delay size RNG
  default_random_engine generator(get_time());
  uniform_int_distribution<size_t> delay_dist(0, ZeroSpeedupWeight + SpeedupDivisions);

  // Initialize the experiment duration
  size_t experiment_length = ExperimentMinTime;

  // Get the starting time for the profiler
  size_t start_time = get_time();

  // Log the start of this execution
  output << "startup\t"
         << "time=" << start_time << "\n";

  // Unblock the main thread
  l.unlock();

  // Wait until there is at least one progress point
  _throughput_points_lock.lock();
  _latency_points_lock.lock();
  while(_throughput_points.size() == 0 && _latency_points.size() == 0 && _running) {
    _throughput_points_lock.unlock();
    _latency_points_lock.unlock();
    wait(ExperimentCoolOffTime);
    _throughput_points_lock.lock();
    _latency_points_lock.lock();
  }
  _throughput_points_lock.unlock();
  _latency_points_lock.unlock();

  // Log sample counts after this many experiments (doubles each time)
  size_t sample_log_interval = 32;
  size_t sample_log_countdown = sample_log_interval;

  // Main experiment loop
  while(_running) {
    // Select a line
    line* selected;
    if(_fixed_line) {   // If this run has a fixed line, use it
      selected = _fixed_line;
    } else {            // Otherwise, wait for the next line to be selected
      selected = _next_line.load();
      while(_running && selected == nullptr) {
        wait(SamplePeriod * SampleBatchSize);
        selected = _next_line.load();
      }

      // If we're no longer running, exit the experiment loop
      if(!_running) break;

      _selected_line.store(selected);
    }

    // Choose a delay size
    size_t delay_size;
    if(_fixed_delay_size >= 0) {
      delay_size = _fixed_delay_size;
    } else {
      size_t r = delay_dist(generator);
      if(r <= ZeroSpeedupWeight) {
        delay_size = 0;
      } else {
        delay_size = (r - ZeroSpeedupWeight) * SamplePeriod / SpeedupDivisions;
      }
    }

    _delay_size.store(delay_size);

    // Save the starting time and sample count
    size_t start_time = get_time();
    size_t starting_samples = selected->get_samples();
    size_t starting_delay_time = _global_delay.load();
    
    // Was arrival speedup enabled?
    int arrival_speedup_percent;
    // Yes. Choose a random arrival speedup size using the same procedure as for line speedup size
    if(_enable_arrival_speedup) {
      size_t r = delay_dist(generator);
      if(r <= ZeroSpeedupWeight) {
        arrival_speedup_percent = 0;
      } else {
        arrival_speedup_percent = (r - ZeroSpeedupWeight) * 100 / SpeedupDivisions;
      }
    }

    // Save throughput point values at the start of the experiment
    vector<unique_ptr<throughput_point::saved>> saved_throughput_points;
    _throughput_points_lock.lock();
    for(pair<const std::string, throughput_point*>& p : _throughput_points) {
//.........这里部分代码省略.........
开发者ID:liuhy1987,项目名称:coz,代码行数:101,代码来源:profiler.cpp

示例5: checkin

 /**
  * Checks in the message.  If the message was checked out for
  * writing, the buffer will be committed and returned to the
  * object_allocator if allow_simultaneous_rw is not set, the
  * buffer will be committing regardless of whether there is an
  * existing reader.  If it is set, the thread will wait until
  * there are no readers before committing the buffer.  Returns the
  * norm of the change of value. (using the norm() function) if its
  * a write checkin. Returns 0 otherwise.
  *
  * \warning writes may block for extended periods if there are
  * readers
  */
 double checkin(object_allocator_tls<F> &pool, 
                const F *msg, const factor_norm<F>& norm, 
                const bool allow_simultaneous_rw) {
   double residual = -1;
   lock.lock();
   // if it matches message address, this was a read request
   if (msg == &message) {
     assert(readercount > 0);
     readercount--;
   }
   //this was a write request
   else if (msg == writebuffer) {
     //Optional: Wait for readers to complete
     if (!allow_simultaneous_rw) {
       while(readercount > 0) {
         lock.unlock();
         sched_yield();
         lock.lock();
       }
     }
     // compute the delta
     residual = norm(message , *(writebuffer));
     message = *(writebuffer);
     pool.checkin(writebuffer);
     writebuffer = NULL;
     lastresidual = residual;
   }
   else {
     //Erroneous message!
     assert(0);
   }
   lock.unlock();
   return residual;
 }
开发者ID:vdeepak13,项目名称:sill,代码行数:47,代码来源:message_data.hpp

示例6: k_heap_compress

// k_heap_compress - Merge free blocks
// This function "compresses" the linked list, consolidating as many adjacent free blocks as possible, to reduce fragmentation.
int k_heap_compress() {
    k_heap_blk* blk = heap.start;
    k_heap_blk* next = NULL;
    int blks_deleted = 0;
    
    __dynmem_lock.lock_cli();
    while((blk != NULL) && (blk->next != NULL)) { // Don't compress the first or last block in the heap.
        next = blk->next;
        if(blk->prev != NULL) {
            if(!blk->used && !blk->prev->used) { // If both the current block and the one before it are free...
                // ...then _delete_ this block. 
                k_heap_delete(blk);
                blks_deleted++;
            }
        }
        blk = next;
    }
    
    if((heap.end->prev != NULL) && (!heap.end->prev->used)) {
        k_heap_delete(heap.end);
        blks_deleted++;
    }
    __dynmem_lock.unlock_cli();
    return blks_deleted;
}
开发者ID:stmobo,项目名称:Omamori,代码行数:27,代码来源:old_malloc.cpp

示例7: kfree

// kfree - free memory block
// This function frees a block of memory given by kmalloc(), allowing other kernel tasks to use it.
void kfree(char* ptr) {
    // given a pointer to a block of memory:
    // find the header for that block of memory
    // set the free bit
    // compress the list
    k_heap_blk *header_ptr = (k_heap_blk*)((size_t)(ptr-sizeof(k_heap_blk)-1));
    __dynmem_lock.lock();
#ifdef DYNMEM_CHECK_FREE_CALLS
    if(header_ptr->magic == HEAP_MAGIC_NUMBER) {
#endif
        header_ptr->used = false;
        if(header_ptr->prev != NULL) {
            if( !(header_ptr->prev->used) ) {
                // Just delete this block.
                k_heap_delete(header_ptr);
            } 
        }
        if(header_ptr->next != NULL) {
            if( !(header_ptr->next->used) ) {
                // Delete header_ptr->next.
                k_heap_blk *next = header_ptr->next;
                k_heap_delete(next);
            }
        }
        
        //k_heap_compress();
#ifdef DYNMEM_CHECK_FREE_CALLS
    } else {
        // We're freeing an invalid pointer.
        panic("dynmem: bad free() call -- could not find magic number\ndynmem: Pointer points to: 0x%x.\n", (unsigned long long int)((size_t)ptr) );
    }
#endif
    __dynmem_lock.unlock();
}
开发者ID:stmobo,项目名称:Omamori,代码行数:36,代码来源:old_malloc.cpp

示例8: kmalloc

char* kmalloc(size_t size) {
    // iterate over every block in the heap list except for the last one
    // and look for a block where the space between the block and the next in the list is at least size bytes...
    // if we can't find one, extend the heap.
    int n_blks = (size / HEAP_BLK_SIZE)+1;
    char* ptr = NULL;
    k_heap_blk* blk = heap.start;
    __dynmem_lock.lock();
    while(blk->next != NULL) {
        if(!blk->used) {
            int blk_sz = ((size_t)blk->next - (size_t)blk);
            if( blk_sz-sizeof(k_heap_blk) >= size ) {
                blk->used = true;
                char* ptr = NULL;
                if( (blk_sz / HEAP_BLK_SIZE) > n_blks ) {
                    k_heap_add_at_offset(blk, n_blks);
                    blk->next->used = false;
                    ptr = (char*)((size_t)blk+sizeof(k_heap_blk)+1);
                } else {
                    ptr = (char*)((size_t)blk+sizeof(k_heap_blk)+1);
                }
                break;
            }
        }
        blk = blk->next;
    }
    if(ptr == NULL) { // if we still haven't allocated memory, then make a new block.
        k_heap_add_at_offset(heap.end, n_blks);
        heap.end->prev->used = true;
        ptr = (char*)((size_t)(heap.end->prev)+sizeof(k_heap_blk)+1);
    }
    __dynmem_lock.unlock();
    return ptr;
}
开发者ID:stmobo,项目名称:Omamori,代码行数:34,代码来源:old_malloc.cpp

示例9: terminal_writestring

// terminal_writestring - print a string to screen
// this function prints a line of text to screen, wrapping and scrolling if necessary.
void terminal_writestring(char* data)
{
    __vga_write_lock.lock();
	size_t datalen = strlen(data);
	for ( size_t i = 0; i < datalen; i++ ) {
		terminal_putchar(data[i]);
    }
    __vga_write_lock.unlock();
}
开发者ID:stmobo,项目名称:Omamori,代码行数:11,代码来源:vga.cpp

示例10: invoke

        virtual void invoke()
        {
#ifdef DEBUG
            s_locker.lock();
            s_fibers.remove(&m_fb->m_link);
            s_locker.unlock();
#endif

            m_fb->m_joins.set();
            m_fb->Unref();
        }
开发者ID:ngot,项目名称:fibjs_vender,代码行数:11,代码来源:fbService.cpp

示例11: terminal_backspace

void terminal_backspace() {
    __vga_write_lock.lock();
    if(--terminal_column > VGA_WIDTH) {
        if(--terminal_row > VGA_HEIGHT) {
            terminal_scroll(-1);
            terminal_row = 0;
        }
    }
    terminal_putentryat(' ', terminal_color, terminal_column, terminal_row);
    __vga_write_lock.unlock();
}
开发者ID:stmobo,项目名称:Omamori,代码行数:11,代码来源:vga.cpp

示例12: find

 bool find(data item) {
     size_t bucket = hashfn(item) % buckets.size();
     lock.lock();
     for (auto it = buckets[bucket].begin(); it != buckets[bucket].end(); it++) {
         if (*it == item) {
             lock.unlock();
             return true;
         }
     }
     lock.unlock();
     return false;
 }
开发者ID:HackBerkeley,项目名称:concurrent-data-structures,代码行数:12,代码来源:hashtable_naive.cpp

示例13: Create

void Service::Create(fiber_func func, void *data, int32_t stacksize, const char* name, Fiber** retVal)
{
    Fiber *fb;
    void **stack;

    stacksize = (stacksize + FB_STK_ALIGN - 1) & ~(FB_STK_ALIGN - 1);
#ifdef WIN32
    fb = (Fiber *) VirtualAlloc(NULL, stacksize, MEM_COMMIT | MEM_TOP_DOWN, PAGE_READWRITE);
#else
    fb = (Fiber *) malloc(stacksize);
#endif
    if (fb == NULL)
        return;

    stack = (void **) fb + stacksize / sizeof(void *) - 5;

    new(fb) Fiber(s_service, data);

    fb->m_cntxt.ip = (intptr_t) fiber_proc;
    fb->m_cntxt.sp = (intptr_t) stack;

#if defined(x64)
#ifdef _WIN32
    fb->m_cntxt.Rcx = (intptr_t) func;
    fb->m_cntxt.Rdx = (intptr_t) fb;
#else
    fb->m_cntxt.Rdi = (intptr_t) func;
    fb->m_cntxt.Rsi = (intptr_t) fb;
#endif
#elif defined(I386)
    stack[1] = (void *)func;
    stack[2] = fb;
#elif defined(arm)
    fb->m_cntxt.r0 = (intptr_t) func;
    fb->m_cntxt.r1 = (intptr_t) fb;
#endif

#ifdef DEBUG
    s_locker.lock();
    s_fibers.putTail(&fb->m_link);
    s_locker.unlock();
#endif

    if (retVal)
    {
        *retVal = fb;
        fb->Ref();
    }

    fb->Ref();
    fb->resume();
}
开发者ID:ngot,项目名称:fibjs_vender,代码行数:52,代码来源:fbService.cpp

示例14: k_heap_add_at_offset

// k_heap_add_at_offset - Add a new heap block
// This function places a new heap block in memory, linked to an "origin" block.
void k_heap_add_at_offset(k_heap_blk* origin_blk, int block_offset) {
    __dynmem_lock.lock_cli();
    k_heap_blk* blk = (k_heap_blk*)((size_t)origin_blk+(block_offset*HEAP_BLK_SIZE));
    blk->prev = origin_blk;
    blk->next = origin_blk->next;
    blk->magic = HEAP_MAGIC_NUMBER;
    
    blk->prev->next = blk;
    if(blk->next != NULL)
        blk->next->prev = blk;
    else
        heap.end = blk;
    __dynmem_lock.unlock_cli();
}
开发者ID:stmobo,项目名称:Omamori,代码行数:16,代码来源:old_malloc.cpp

示例15: forEach

void Service::forEach(void (*func)(Fiber*))
{
    s_locker.lock();

    linkitem* p = s_fibers.head();

    while (p)
    {
        Fiber* zfb = 0;
        func((Fiber*)((intptr_t)p - (intptr_t)(&zfb->m_link)));

        p = s_fibers.next(p);
    }

    s_locker.unlock();
}
开发者ID:ngot,项目名称:fibjs_vender,代码行数:16,代码来源:fbService.cpp


注:本文中的spinlock类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。