亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频

? 歡迎來到蟲蟲下載站! | ?? 資源下載 ?? 資源專輯 ?? 關于我們
? 蟲蟲下載站

?? nt.cpp

?? tightvnc源碼
?? CPP
?? 第 1 頁 / 共 2 頁
字號:
//				Package : omnithread
// omnithread/nt.cc		Created : 6/95 tjr
//
//    Copyright (C) 1999 AT&T Laboratories Cambridge. All Rights Reserved.
//
//    This file is part of the omnithread library
//
//    The omnithread library is free software; you can redistribute it and/or
//    modify it under the terms of the GNU Library General Public
//    License as published by the Free Software Foundation; either
//    version 2 of the License, or (at your option) any later version.
//
//    This library is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//    Library General Public License for more details.
//
//    You should have received a copy of the GNU Library General Public
//    License along with this library; if not, write to the Free
//    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  
//    02111-1307, USA
//

//
// Implementation of OMNI thread abstraction for NT threads
//

#include <stdlib.h>
#include <errno.h>
#include "omnithread.h"
#include <process.h>

#define DB(x) // x 
//#include <iostream.h> or #include <iostream> if DB is on.

static void get_time_now(unsigned long* abs_sec, unsigned long* abs_nsec);

///////////////////////////////////////////////////////////////////////////
//
// Mutex
//
///////////////////////////////////////////////////////////////////////////


omni_mutex::omni_mutex(void)
{
    InitializeCriticalSection(&crit);
}

omni_mutex::~omni_mutex(void)
{
    DeleteCriticalSection(&crit);
}

void
omni_mutex::lock(void)
{
    EnterCriticalSection(&crit);
}

void
omni_mutex::unlock(void)
{
    LeaveCriticalSection(&crit);
}



///////////////////////////////////////////////////////////////////////////
//
// Condition variable
//
///////////////////////////////////////////////////////////////////////////


//
// Condition variables are tricky to implement using NT synchronisation
// primitives, since none of them have the atomic "release mutex and wait to be
// signalled" which is central to the idea of a condition variable.  To get
// around this the solution is to record which threads are waiting and
// explicitly wake up those threads.
//
// Here we implement a condition variable using a list of waiting threads
// (protected by a critical section), and a per-thread semaphore (which
// actually only needs to be a binary semaphore).
//
// To wait on the cv, a thread puts itself on the list of waiting threads for
// that cv, then releases the mutex and waits on its own personal semaphore.  A
// signalling thread simply takes a thread from the head of the list and kicks
// that thread's semaphore.  Broadcast is simply implemented by kicking the
// semaphore of each waiting thread.
//
// The only other tricky part comes when a thread gets a timeout from a timed
// wait on its semaphore.  Between returning with a timeout from the wait and
// entering the critical section, a signalling thread could get in, kick the
// waiting thread's semaphore and remove it from the list.  If this happens,
// the waiting thread's semaphore is now out of step so it needs resetting, and
// the thread should indicate that it was signalled rather than that it timed
// out.
//
// It is possible that the thread calling wait or timedwait is not a
// omni_thread. In this case we have to provide a temporary data structure,
// i.e. for the duration of the call, for the thread to link itself on the
// list of waiting threads. _internal_omni_thread_dummy provides such
// a data structure and _internal_omni_thread_helper is a helper class to
// deal with this special case for wait() and timedwait(). Once created,
// the _internal_omni_thread_dummy is cached for use by the next wait() or
// timedwait() call from a non-omni_thread. This is probably worth doing
// because creating a Semaphore is quite heavy weight.

class _internal_omni_thread_helper;

class _internal_omni_thread_dummy : public omni_thread {
public:
  inline _internal_omni_thread_dummy() : next(0) { }
  inline ~_internal_omni_thread_dummy() { }
  friend class _internal_omni_thread_helper;
private:
  _internal_omni_thread_dummy* next;
};

class _internal_omni_thread_helper {
public:
  inline _internal_omni_thread_helper()  { 
    d = 0;
    t = omni_thread::self();
    if (!t) {
      omni_mutex_lock sync(cachelock);
      if (cache) {
	d = cache;
	cache = cache->next;
      }
      else {
	d = new _internal_omni_thread_dummy;
      }
      t = d;
    }
  }
  inline ~_internal_omni_thread_helper() { 
    if (d) {
      omni_mutex_lock sync(cachelock);
      d->next = cache;
      cache = d;
    }
  }
  inline operator omni_thread* () { return t; }
  inline omni_thread* operator->() { return t; }

  static _internal_omni_thread_dummy* cache;
  static omni_mutex                   cachelock;

private:
  _internal_omni_thread_dummy* d;
  omni_thread*                 t;
};

_internal_omni_thread_dummy* _internal_omni_thread_helper::cache = 0;
omni_mutex                   _internal_omni_thread_helper::cachelock;


omni_condition::omni_condition(omni_mutex* m) : mutex(m)
{
    InitializeCriticalSection(&crit);
    waiting_head = waiting_tail = NULL;
}


omni_condition::~omni_condition(void)
{
    DeleteCriticalSection(&crit);
    DB( if (waiting_head != NULL) {
	cerr << "omni_condition::~omni_condition: list of waiting threads "
	     << "is not empty\n";
    } )
}


void
omni_condition::wait(void)
{
    _internal_omni_thread_helper me;

    EnterCriticalSection(&crit);

    me->cond_next = NULL;
    me->cond_prev = waiting_tail;
    if (waiting_head == NULL)
	waiting_head = me;
    else
	waiting_tail->cond_next = me;
    waiting_tail = me;
    me->cond_waiting = TRUE;

    LeaveCriticalSection(&crit);

    mutex->unlock();

    DWORD result = WaitForSingleObject(me->cond_semaphore, INFINITE);

    mutex->lock();

    if (result != WAIT_OBJECT_0)
	throw omni_thread_fatal(GetLastError());
}


int
omni_condition::timedwait(unsigned long abs_sec, unsigned long abs_nsec)
{
    _internal_omni_thread_helper me;

    EnterCriticalSection(&crit);

    me->cond_next = NULL;
    me->cond_prev = waiting_tail;
    if (waiting_head == NULL)
	waiting_head = me;
    else
	waiting_tail->cond_next = me;
    waiting_tail = me;
    me->cond_waiting = TRUE;

    LeaveCriticalSection(&crit);

    mutex->unlock();

    unsigned long now_sec, now_nsec;

    get_time_now(&now_sec, &now_nsec);

    DWORD timeout = (abs_sec-now_sec) * 1000 + (abs_nsec-now_nsec) / 1000000;

    if ((abs_sec <= now_sec) && ((abs_sec < now_sec) || (abs_nsec < abs_nsec)))
	timeout = 0;

    DWORD result = WaitForSingleObject(me->cond_semaphore, timeout);

    if (result == WAIT_TIMEOUT) {
	EnterCriticalSection(&crit);

	if (me->cond_waiting) {
	    if (me->cond_prev != NULL)
		me->cond_prev->cond_next = me->cond_next;
	    else
		waiting_head = me->cond_next;
	    if (me->cond_next != NULL)
		me->cond_next->cond_prev = me->cond_prev;
	    else
		waiting_tail = me->cond_prev;
	    me->cond_waiting = FALSE;

	    LeaveCriticalSection(&crit);

	    mutex->lock();
	    return 0;
	}

	//
	// We timed out but another thread still signalled us.  Wait for
	// the semaphore (it _must_ have been signalled) to decrement it
	// again.  Return that we were signalled, not that we timed out.
	//

	LeaveCriticalSection(&crit);

	result = WaitForSingleObject(me->cond_semaphore, INFINITE);
    }

    if (result != WAIT_OBJECT_0)
	throw omni_thread_fatal(GetLastError());

    mutex->lock();
    return 1;
}


void
omni_condition::signal(void)
{
    EnterCriticalSection(&crit);

    if (waiting_head != NULL) {
	omni_thread* t = waiting_head;
	waiting_head = t->cond_next;
	if (waiting_head == NULL)
	    waiting_tail = NULL;
	else
	    waiting_head->cond_prev = NULL;
	t->cond_waiting = FALSE;

	if (!ReleaseSemaphore(t->cond_semaphore, 1, NULL)) {
	    int rc = GetLastError();
	    LeaveCriticalSection(&crit);
	    throw omni_thread_fatal(rc);
	}
    }

    LeaveCriticalSection(&crit);
}


void
omni_condition::broadcast(void)
{
    EnterCriticalSection(&crit);

    while (waiting_head != NULL) {
	omni_thread* t = waiting_head;
	waiting_head = t->cond_next;
	if (waiting_head == NULL)
	    waiting_tail = NULL;
	else
	    waiting_head->cond_prev = NULL;
	t->cond_waiting = FALSE;

	if (!ReleaseSemaphore(t->cond_semaphore, 1, NULL)) {
	    int rc = GetLastError();
	    LeaveCriticalSection(&crit);
	    throw omni_thread_fatal(rc);
	}
    }

    LeaveCriticalSection(&crit);
}



///////////////////////////////////////////////////////////////////////////
//
// Counting semaphore
//
///////////////////////////////////////////////////////////////////////////


#define SEMAPHORE_MAX 0x7fffffff


omni_semaphore::omni_semaphore(unsigned int initial)
{
    nt_sem = CreateSemaphore(NULL, initial, SEMAPHORE_MAX, NULL);

    if (nt_sem == NULL) {
      DB( cerr << "omni_semaphore::omni_semaphore: CreateSemaphore error "
	     << GetLastError() << endl );
      throw omni_thread_fatal(GetLastError());
    }
}


omni_semaphore::~omni_semaphore(void)
{
  if (!CloseHandle(nt_sem)) {
    DB( cerr << "omni_semaphore::~omni_semaphore: CloseHandle error "
	     << GetLastError() << endl );
    throw omni_thread_fatal(GetLastError());
  }
}


void
omni_semaphore::wait(void)
{
    if (WaitForSingleObject(nt_sem, INFINITE) != WAIT_OBJECT_0)
	throw omni_thread_fatal(GetLastError());
}


int
omni_semaphore::trywait(void)
{
    switch (WaitForSingleObject(nt_sem, 0)) {

    case WAIT_OBJECT_0:
	return 1;
    case WAIT_TIMEOUT:
	return 0;
    }

    throw omni_thread_fatal(GetLastError());
    return 0; /* keep msvc++ happy */
}


void
omni_semaphore::post(void)
{
    if (!ReleaseSemaphore(nt_sem, 1, NULL))
	throw omni_thread_fatal(GetLastError());
}



///////////////////////////////////////////////////////////////////////////
//
// Thread
//
///////////////////////////////////////////////////////////////////////////


//
// Static variables
//

int omni_thread::init_t::count = 0;

omni_mutex* omni_thread::next_id_mutex;
int omni_thread::next_id = 0;
static DWORD self_tls_index;

//
// Initialisation function (gets called before any user code).
//

omni_thread::init_t::init_t(void)
{
    if (count++ != 0)	// only do it once however many objects get created.
	return;

?? 快捷鍵說明

復制代碼 Ctrl + C
搜索代碼 Ctrl + F
全屏模式 F11
切換主題 Ctrl + Shift + D
顯示快捷鍵 ?
增大字號 Ctrl + =
減小字號 Ctrl + -
亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频
国产一区二区在线看| 精品视频资源站| 亚洲电影一级黄| 国产欧美日韩亚州综合| 色噜噜狠狠色综合中国| 精品一区免费av| 婷婷综合另类小说色区| 一区二区三区国产精品| 久久久精品国产99久久精品芒果| 成人av电影在线网| 午夜精品久久久| 久久久亚洲综合| 久久久精品一品道一区| 99久久精品免费| 国产剧情在线观看一区二区| 国产精品青草综合久久久久99| 91免费观看视频在线| 亚洲精品在线三区| 成人美女在线观看| 丁香婷婷深情五月亚洲| 最新国产の精品合集bt伙计| 一道本成人在线| 日本一区中文字幕| xvideos.蜜桃一区二区| 国产sm精品调教视频网站| 中文字幕巨乱亚洲| 91啪亚洲精品| 欧美电影一区二区三区| 欧美综合色免费| 色婷婷av一区| 日韩午夜在线影院| 国产丝袜欧美中文另类| 国产欧美日韩综合| 亚洲精品精品亚洲| 麻豆精品一区二区三区| 国产一区二区三区四区五区美女| 成人伦理片在线| 欧美视频在线播放| 精品少妇一区二区三区日产乱码| 国产精品嫩草影院com| 亚洲午夜精品在线| 国产精品乡下勾搭老头1| 一本高清dvd不卡在线观看| 不卡一区二区在线| 欧美日韩视频在线一区二区| xnxx国产精品| 同产精品九九九| www.日韩av| 久久嫩草精品久久久久| 亚洲成在线观看| 欧美亚洲一区二区在线| 亚洲欧洲美洲综合色网| 成人性视频免费网站| 国产精品的网站| 国产精品视频一二三区| 亚洲欧洲综合另类在线| 色综合天天性综合| 精品无人码麻豆乱码1区2区| 日韩欧美在线不卡| 男人操女人的视频在线观看欧美| 久久久噜噜噜久久中文字幕色伊伊| 91网站最新网址| 婷婷开心久久网| 国产精品久久久久7777按摩| 3atv一区二区三区| 日本道免费精品一区二区三区| 亚洲成在人线在线播放| 国产精品久久久久久一区二区三区| 国产成人亚洲精品狼色在线| 欧美久久久一区| 一区二区三区国产精品| 制服丝袜日韩国产| 久久精品国产免费看久久精品| 91免费看片在线观看| 国产精品高潮呻吟久久| 成人动漫中文字幕| 亚洲乱码日产精品bd| 国产精品 欧美精品| 久久综合九色综合97婷婷女人 | 久久新电视剧免费观看| 精品一区二区三区免费毛片爱| 日韩精品中文字幕一区| 国产精品一二三四五| 中文字幕欧美三区| 国产suv一区二区三区88区| 日韩女优毛片在线| 91免费观看在线| 国产一二三精品| 亚洲一区二区三区四区在线免费观看| 国产欧美日韩亚州综合| 欧美撒尿777hd撒尿| 99在线精品一区二区三区| 日本一区二区电影| 成人av在线播放网址| 蜜臀av性久久久久av蜜臀妖精| 国内偷窥港台综合视频在线播放| 东方aⅴ免费观看久久av| 一区二区高清免费观看影视大全 | 在线亚洲精品福利网址导航| 欧美在线视频你懂得| 欧美在线一二三四区| 在线观看欧美精品| 欧美日韩一二三| 一本色道亚洲精品aⅴ| 9i在线看片成人免费| 成人深夜视频在线观看| 粉嫩av一区二区三区粉嫩| 亚洲欧美综合在线精品| 亚洲综合一区在线| 日韩在线一区二区三区| 精品无人码麻豆乱码1区2区| 高清不卡一区二区在线| 不卡电影免费在线播放一区| 91麻豆文化传媒在线观看| 欧美主播一区二区三区| 精品国产乱码久久久久久久久| 国产丝袜欧美中文另类| 中文字幕亚洲成人| 亚洲精品国产一区二区精华液| 亚洲午夜久久久久中文字幕久| 美女尤物国产一区| 国产91对白在线观看九色| 婷婷综合在线观看| 成人永久aaa| 欧美亚洲一区二区三区四区| 久久精品视频免费观看| 亚洲欧美日韩精品久久久久| 亚洲一区二区三区爽爽爽爽爽| 麻豆中文一区二区| 成人三级伦理片| 91精品在线免费观看| 中文字幕欧美激情一区| 免费精品视频在线| 不卡电影一区二区三区| 精品捆绑美女sm三区| 1024精品合集| 亚洲一区二区三区不卡国产欧美 | 日韩欧美另类在线| 国产99久久精品| 日韩亚洲欧美综合| 国产精品每日更新在线播放网址| 日本一不卡视频| 99久久精品国产一区| 久久久噜噜噜久久中文字幕色伊伊| 亚洲摸摸操操av| 青青草91视频| 欧美日韩精品综合在线| 国产精品美女久久福利网站| 激情五月激情综合网| 欧美一a一片一级一片| 国产精品视频线看| 蜜桃精品视频在线观看| 欧美日韩在线免费视频| 国产精品人成在线观看免费| 午夜久久久久久久久久一区二区| 99精品国产视频| 欧美精品一区二区高清在线观看| 一区二区三区在线免费| 久久成人久久鬼色| 91久久人澡人人添人人爽欧美| 国产女人18毛片水真多成人如厕| 日韩av电影一区| 欧美人妇做爰xxxⅹ性高电影 | 欧美日韩成人综合天天影院| 亚洲人成在线播放网站岛国| 国产麻豆午夜三级精品| 欧美一区二区三区视频在线观看| 一区二区不卡在线播放 | 欧美一区二区三区视频免费播放| 亚洲一级不卡视频| 99精品国产91久久久久久| wwwwww.欧美系列| 美女性感视频久久| 成人永久免费视频| 国产精品伦一区| 东方aⅴ免费观看久久av| 国产精品高潮呻吟| 成人精品在线视频观看| 一色屋精品亚洲香蕉网站| 国产精品资源站在线| 国产精品色哟哟网站| 成人三级在线视频| 亚洲视频一区二区在线观看| 成人av电影免费在线播放| 亚洲另类在线制服丝袜| 色婷婷国产精品| 香蕉久久夜色精品国产使用方法 | 一区二区三区在线观看欧美| 一本久道中文字幕精品亚洲嫩| 亚洲线精品一区二区三区| 在线观看欧美日本| 久久精品理论片| 久久综合成人精品亚洲另类欧美 | 国产亚洲欧美激情| 不卡的av电影| 一区二区在线观看av| 欧美一三区三区四区免费在线看 | 欧美福利电影网| 日韩精彩视频在线观看| 久久久久久久综合|