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

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

?? nt.cpp

?? tightvnc 1.3.9 的源代碼
?? 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网站免费线看精品| 日韩亚洲电影在线| 日韩高清中文字幕一区| 欧美精品一区二区三区蜜桃 | 国产日韩欧美一区二区三区乱码| 精品系列免费在线观看| 自拍av一区二区三区| 91麻豆精品国产自产在线观看一区 | 欧美日韩和欧美的一区二区| 日韩福利电影在线| 亚洲欧美影音先锋| 56国语精品自产拍在线观看| 成人h动漫精品一区二区| 日韩精品亚洲专区| 久久久国产精华| 亚洲精品水蜜桃| 99视频精品在线| 国产老妇另类xxxxx| 日韩成人一区二区| 亚洲女人的天堂| 国产欧美精品区一区二区三区| 欧美高清性hdvideosex| 一本大道av一区二区在线播放| 从欧美一区二区三区| 久久成人免费网站| 日韩精品91亚洲二区在线观看| 亚洲人吸女人奶水| 中文字幕在线一区二区三区| 久久久久久久综合日本| 精品国产123| 欧美va亚洲va| 日韩欧美一区中文| 日韩一区二区三区四区五区六区| 91精品国产免费久久综合| 色琪琪一区二区三区亚洲区| jlzzjlzz欧美大全| www.亚洲色图| 99视频精品在线| 91视频在线看| 91黄色激情网站| 欧美色倩网站大全免费| 欧美午夜寂寞影院| 欧美精品少妇一区二区三区| 欧美老女人在线| 欧美videos大乳护士334| 精品人在线二区三区| 欧美大片在线观看| 日韩欧美123| 精品国产91九色蝌蚪| 国产天堂亚洲国产碰碰| 久久蜜桃香蕉精品一区二区三区| 精品久久久网站| 国产免费观看久久| 中文字幕亚洲一区二区av在线 | 欧美高清视频不卡网| 欧美三级在线看| 6080国产精品一区二区| 精品国产露脸精彩对白| 国产午夜一区二区三区| 国产精品超碰97尤物18| 亚洲美女视频在线| 亚洲超碰精品一区二区| 蜜臀av亚洲一区中文字幕| 国产美女一区二区| 成人av在线观| 欧美日韩一区二区三区在线 | 午夜激情综合网| 美女性感视频久久| 国产精品18久久久久久久久久久久| 国产经典欧美精品| 一本久久综合亚洲鲁鲁五月天| 欧美中文字幕不卡| 日韩欧美精品在线| 国产精品天美传媒沈樵| 亚洲一区二区三区爽爽爽爽爽| 麻豆国产一区二区| 欧美日韩免费电影| 日韩欧美电影一区| 中文一区二区在线观看| 亚洲一二三级电影| 国产一区在线观看视频| 在线亚洲人成电影网站色www| 日韩美女在线视频| 亚洲老司机在线| 精品一区二区影视| 色乱码一区二区三区88| 精品国产91乱码一区二区三区| 亚洲日本在线a| 老司机精品视频导航| 91成人免费在线| 国产视频视频一区| 日韩成人一级大片| 91丝袜高跟美女视频| 欧美xingq一区二区| 一区二区三区国产精品| 国产一区91精品张津瑜| 欧美日韩精品欧美日韩精品一综合| 久久久久久综合| 日韩成人免费在线| 一本色道久久综合亚洲91| 久久婷婷国产综合国色天香| 婷婷亚洲久悠悠色悠在线播放 | 51午夜精品国产| 亚洲卡通动漫在线| 国产成人小视频| 欧美一级高清大全免费观看| 一区二区成人在线观看| 成人av综合一区| 久久新电视剧免费观看| 免费在线观看一区| 欧美亚洲禁片免费| 亚洲女同一区二区| 成人黄色在线看| 国产欧美日韩麻豆91| 精品一区二区三区免费观看| 欧美美女一区二区| 亚洲专区一二三| 91福利区一区二区三区| 亚洲欧美在线视频| 成人国产精品免费| 欧美激情一区二区| 国产精品白丝jk黑袜喷水| 日韩精品中文字幕一区二区三区| 天天综合色天天| 欧美日韩国产不卡| 亚洲一区二区中文在线| 色综合久久88色综合天天6| 国产精品网站在线播放| 国产成人8x视频一区二区| 久久精品欧美一区二区三区不卡| 麻豆专区一区二区三区四区五区| 91精品国产入口在线| 奇米色一区二区| 日韩精品最新网址| 久久精品国产久精国产爱| 日韩视频免费直播| 精品伊人久久久久7777人| 精品播放一区二区| 国产精品996| 国产精品毛片a∨一区二区三区| 国产91对白在线观看九色| 中文字幕成人在线观看| 成人福利视频网站| 亚洲少妇最新在线视频| 一本大道av伊人久久综合| 亚洲国产精品影院| 337p亚洲精品色噜噜| 久久精品国产精品亚洲红杏| 精品电影一区二区三区| 福利一区二区在线观看| 一区二区三区四区av| 欧美视频在线观看一区二区| 欧美aaaaaa午夜精品| 亚洲精品在线电影| 成人免费毛片aaaaa**| 一区二区三区四区不卡在线 | 国产精品久久久久久久久快鸭| jlzzjlzz亚洲女人18| 亚洲自拍偷拍网站| 日韩欧美在线1卡| 东方aⅴ免费观看久久av| 国产精品麻豆久久久| 欧美怡红院视频| 精品一区精品二区高清| 亚洲色欲色欲www| 欧美群妇大交群中文字幕| 国产在线精品免费| 一区二区三区四区中文字幕| 日韩欧美在线1卡| 国产91露脸合集magnet | 日韩精品一二三| 久久在线观看免费| 97久久精品人人爽人人爽蜜臀| 夜夜嗨av一区二区三区网页| 日韩午夜精品电影| 色综合久久久久| 国模娜娜一区二区三区| 亚洲欧美色图小说| 欧美mv和日韩mv的网站| 91香蕉视频污在线| 免费一级欧美片在线观看| 欧美国产综合色视频| 精品婷婷伊人一区三区三| 国产精品88888| 日韩电影一区二区三区四区| 中文字幕精品在线不卡| 精品久久久久久最新网址| 国产精品综合在线视频| 欧美在线观看禁18| 亚洲综合在线五月| 欧美性猛交xxxxxx富婆| 久久99蜜桃精品| 亚洲色图.com| 欧美精品一区二区三区久久久| 一本久道久久综合中文字幕| 丝袜亚洲另类丝袜在线| 中文子幕无线码一区tr| 欧美成人高清电影在线| 国产成人精品www牛牛影视|