예제 #1
0
파일: main.cpp 프로젝트: CCJY/coliru
	// taskFunction: Task Function.
	//          arg: argument that will be passed to taskFunction. 
	void Execute(TaskFunction taskFunction, void *arg) {
		if (corePoolSize_ > currentThreads_) {
			NewThread();
			std::cout << "if (corePoolSize_ > currentThreads_) {" << '\n';
		} else if (currentThreads_ < maximumPoolSize_ && taskQueueSize_ + 1 > currentThreads_) {
			NewThread();
			std::cout << "} else if (currentThreads_ < maximumPoolSize_ && taskQueueSize_ + 1 > currentThreads_) {" << '\n';
		}

		Task newTask;
		newTask.taskFunction = taskFunction;
		newTask.arg = arg;

		while (!taskQueue_.push(newTask))
			;


	//	std::cout<<"push task ok.\n";

		++ taskQueueSize_;
		conditionVariable_.notify_one();
//		conditionVariable_.notify_all();

		std::cout<<"notify.\n";
	}
예제 #2
0
파일: IPsec.c 프로젝트: macvk/SoftEtherVPN
// Initialize the IPsec server
IPSEC_SERVER *NewIPsecServer(CEDAR *cedar)
{
	IPSEC_SERVER *s;
	// Validate arguments
	if (cedar == NULL)
	{
		return NULL;
	}

	s = ZeroMalloc(sizeof(IPSEC_SERVER));

	s->LockSettings = NewLock();

	s->Cedar = cedar;

	AddRef(s->Cedar->ref);

	s->L2TP = NewL2TPServer(cedar);

	s->Ike = NewIKEServer(cedar, s);
	StrCpy(s->Ike->Secret, sizeof(s->Ike->Secret), IPSEC_DEFAULT_SECRET);

	s->UdpListener = NewUdpListener(IPsecServerUdpPacketRecvProc, s, &cedar->Server->ListenIP);

	s->EtherIPIdList = NewList(CmpEtherIPId);

	// Start an OS service monitoring thread
	s->OsServiceCheckThreadEvent = NewEvent();
	s->OsServiceCheckThread = NewThread(IPsecOsServiceCheckThread, s);

	return s;
}
예제 #3
0
// Initialize the packet adapter
bool LinkPaInit(SESSION *s)
{
	LINK *k;
	THREAD *t;
	// Validate arguments
	if (s == NULL || (k = (LINK *)s->PacketAdapter->Param) == NULL)
	{
		return false;
	}

	if (k->Halting || (*k->StopAllLinkFlag))
	{
		return false;
	}

	// Create a transmission packet queue
	k->SendPacketQueue = NewQueue();

	// Creat a link server thread
	t = NewThread(LinkServerSessionThread, (void *)k);
	WaitThreadInit(t);

	k->LastServerConnectionReceivedBlocksNum = 0;
	k->CurrentSendPacketQueueSize = 0;

	ReleaseThread(t);

	return true;
}
예제 #4
0
파일: thread.cpp 프로젝트: gitrider/wxsj2
bool wxThreadInternal::Create(wxThread *thread, unsigned int stackSize)
{
    if ( s_threadEntry == NULL )
    {
        s_threadEntry = NewThreadEntryUPP( (ThreadEntryProcPtr) MacThreadStart ) ;
    }
    OSErr err = NewThread( kCooperativeThread,
                           s_threadEntry,
                           (void*) thread,
                           stackSize,
                           kNewSuspend,
                           &m_result,
                           &m_tid );

    if ( err != noErr )
    {
        wxLogSysError(_("Can't create thread"));
        return FALSE;
    }

    if ( m_priority != WXTHREAD_DEFAULT_PRIORITY )
    {
        SetPriority(m_priority);
    }

    m_state = STATE_NEW;

    return TRUE;
}
예제 #5
0
// Restarting the server
void EiRebootServer()
{
	THREAD *t;

	t = NewThread(EiRebootServerThread, NULL);
	ReleaseThread(t);
}
예제 #6
0
// Create a new SecureNAT
SNAT *SnNewSecureNAT(HUB *h, VH_OPTION *o)
{
	SNAT *s;
	THREAD *t;
	// Validate arguments
	if (h == NULL || o == NULL)
	{
		return NULL;
	}

	s = ZeroMalloc(sizeof(SNAT));
	s->Cedar = h->Cedar;
	s->Hub = h;
	s->lock = NewLock();

	// Create a NAT
	s->Nat = NiNewNatEx(s, o);

	// Initialize the virtual machine
	VirtualInit(s->Nat->Virtual);

	// Create a thread
	t = NewThread(SnSecureNATThread, s);
	WaitThreadInit(t);
	ReleaseThread(t);

	return s;
}
예제 #7
0
// Initialize the packet adapter
bool NullPaInit(SESSION *s)
{
	NULL_LAN *n;
	static UINT id_seed = 0;
	// Validate arguments
	if (s == NULL)
	{
		return false;
	}

	id_seed++;

	n = ZeroMalloc(sizeof(NULL_LAN));
	n->Id = id_seed;
	s->PacketAdapter->Param = (void *)n;

	n->Cancel = NewCancel();
	n->PacketQueue = NewQueue();
	n->Event = NewEvent();

	NullGenerateMacAddress(n->MacAddr, n->Id, 0);

	n->PacketGeneratorThread = NewThread(NullPacketGenerateThread, n);

	return true;
}
예제 #8
0
// Start a Layer-3 switch
void L3SwStart(L3SW *s)
{
	// Validate arguments
	if (s == NULL)
	{
		return;
	}

	Lock(s->lock);
	{
		if (s->Active == false)
		{
			// Start if there is registered interface
			if (LIST_NUM(s->IfList) >= 1)
			{
				s->Halt = false;

				// Create a thread
				s->Thread = NewThread(L3SwThread, s);
				WaitThreadInit(s->Thread);
			}
		}
	}
	Unlock(s->lock);
}
예제 #9
0
// Create new VPN Azure client
AZURE_CLIENT *NewAzureClient(CEDAR *cedar, SERVER *server)
{
	AZURE_CLIENT *ac;
	// Validate arguments
	if (cedar == NULL || server == NULL)
	{
		return NULL;
	}

	ac = ZeroMalloc(sizeof(AZURE_CLIENT));

	ac->Cedar = cedar;

	ac->Server = server;

	ac->Lock = NewLock();

	ac->IsEnabled = false;

	ac->Event = NewEvent();

	// Start main thread
	ac->MainThread = NewThread(AcMainThread, ac);

	return ac;
}
예제 #10
0
// Jump here if there is accepted connection in the TCP
void TCPAccepted(LISTENER *r, SOCK *s)
{
	TCP_ACCEPTED_PARAM *data;
	THREAD *t;
	char tmp[MAX_SIZE];
	UINT num_clients_from_this_ip = 0;
	CEDAR *cedar;
	// Validate arguments
	if (r == NULL || s == NULL)
	{
		return;
	}

	cedar = r->Cedar;

	num_clients_from_this_ip = GetNumIpClient(&s->RemoteIP);


	IPToStr(tmp, sizeof(tmp), &s->RemoteIP);

	data = ZeroMalloc(sizeof(TCP_ACCEPTED_PARAM));
	data->r = r;
	data->s = s;

	if (r->ThreadProc == TCPAcceptedThread)
	{
		Inc(cedar->AcceptingSockets);
	}

	t = NewThread(r->ThreadProc, data);
	WaitThreadInit(t);
	Free(data);
	ReleaseThread(t);
}
예제 #11
0
NetDownloader *FD_LoadPlugin()
{
    FileDownload *dnload;
    NetDownloader *plug = malloc(sizeof(NetDownloader));
    memset(plug, 0, sizeof(NetDownloader));
    M4_REG_PLUG(plug, M4FILEDOWNLOADER, "HTTP Downloader", "gpac distribution", 0);

    plug->CanHandleURL = FD_CanHandleURL;
    plug->GetMimeType = FD_GetMimeType;
    plug->Connect = FD_Connect;
    plug->FetchData = FD_FetchData;
    plug->Close= FD_Close;
    plug->GetCacheFileName = FD_GetCacheFileName;

    dnload = malloc(sizeof(FileDownload));
    memset(dnload, 0, sizeof(FileDownload));
    dnload->service_thread = NewThread();
    plug->net_status = DL_Unavailable;

    plug->priv = dnload;

    /*also init clock*/
    M4_InitClock();
    return plug;
}
예제 #12
0
BaseDecoder *NewISCodec(u32 PL)
{
	ISPriv *priv;
	SceneDecoder *tmp;
	
	tmp = (SceneDecoder*) malloc(sizeof(SceneDecoder));
	if (!tmp) return NULL;
	memset(tmp, 0, sizeof(SceneDecoder));

	priv = (ISPriv *) malloc(sizeof(ISPriv));
	memset(priv, 0, sizeof(ISPriv));

	priv->is_nodes = NewChain();
	priv->ddf = NewChain();

	tmp->privateStack = priv;

	tmp->AttachStream = IS_AttachStream;
	tmp->DetachStream = IS_DetachStream;
	tmp->GetCapabilities = IS_GetCapabilities;
	tmp->SetCapabilities = IS_SetCapabilities;
	tmp->ProcessData = IS_ProcessData;
	/*we don't use this...*/
	tmp->AttachScene = NULL;

	M4_REG_PLUG(tmp, M4SCENEDECODERINTERFACE, "GPAC InputSensor Decoder", "gpac distribution", 0)

#if M4_HTK_DEMO
	priv->th = NewThread();
#endif
	return (BaseDecoder *) tmp;
}
예제 #13
0
// Create a new stack
NATIVE_STACK *NewNativeStack(CEDAR *cedar, char *device_name, char *mac_address_seed)
{
	ETH *eth;
	NATIVE_STACK *a;
	IP localhost;
	char tmp[64];
	bool release_cedar = false;
	// Validate arguments
	if (device_name == NULL || mac_address_seed == NULL)
	{
		return NULL;
	}

	GetLocalHostIP4(&localhost);

	// Open the Eth device
	eth = OpenEth(device_name, false, false, NULL);
	if (eth == NULL)
	{
		return NULL;
	}

	if (cedar == NULL)
	{
		cedar = NewCedar(NULL, NULL);
		release_cedar = true;
	}

	a = ZeroMalloc(sizeof(NATIVE_STACK));

	NewSocketPair(&a->Sock1, &a->Sock2, &localhost, 1, &localhost, 1);

	a->Cedar = cedar;
	AddRef(a->Cedar->ref);

	NsGenMacAddress(a->MacAddress, mac_address_seed, device_name);

	BinToStr(tmp, sizeof(tmp), a->MacAddress, sizeof(a->MacAddress));
	Debug("NewNativeStack: MAC Address = %s\n", tmp);

	a->Ipc = NewIPCBySock(cedar, a->Sock2, a->MacAddress);

	StrCpy(a->DeviceName, sizeof(a->DeviceName), device_name);

	a->Eth = eth;
	a->Cancel = EthGetCancel(eth);

	a->MainThread = NewThread(NsMainThread, a);

	if (release_cedar)
	{
		ReleaseCedar(cedar);
	}

	a->IsIpRawMode = a->Eth->IsRawIpMode;

	return a;
}
예제 #14
0
파일: worker.c 프로젝트: blakawk/Aio4c
Worker* NewWorker(char* pipeName, aio4c_size_t bufferSize) {
    Worker* worker = NULL;
    ErrorCode code = AIO4C_ERROR_CODE_INITIALIZER;

    if ((worker = aio4c_malloc(sizeof(Worker))) == NULL) {
#ifndef AIO4C_WIN32
        code.error = errno;
#else /* AIO4C_WIN32 */
        code.source = AIO4C_ERRNO_SOURCE_SYS;
#endif /* AIO4C_WIN32 */
        code.size = sizeof(Worker);
        code.type = "Worker";
        Raise(AIO4C_LOG_LEVEL_ERROR, AIO4C_ALLOC_ERROR_TYPE, AIO4C_ALLOC_ERROR, &code);
        return NULL;
    }

    worker->queue      = NULL;
    worker->pool       = NULL;
    worker->writer     = NULL;
    worker->bufferSize = bufferSize;

    if (pipeName != NULL) {
        worker->pipe       = pipeName;
        worker->name       = aio4c_malloc(strlen(pipeName) + 1 + 7);
        if (worker->name != NULL) {
            snprintf(worker->name, strlen(pipeName) + 1 + 7, "%s-worker", pipeName);
        }
    } else {
        worker->pipe = NULL;
        worker->name = NULL;
    }

    worker->thread = NewThread(
            worker->name,
            _WorkerInit,
            _WorkerRun,
            _WorkerExit,
            (ThreadData)worker);

    if (worker->thread == NULL) {
        if (worker->name != NULL) {
            aio4c_free(worker->name);
        }
        aio4c_free(worker);
        return NULL;
    }

    if (!ThreadStart(worker->thread)) {
        if (worker->name != NULL) {
            aio4c_free(worker->name);
        }
        aio4c_free(worker);
        return NULL;
    }

    return worker;
}
예제 #15
0
void
CPoolOfThreads_ForServer::Spawn(unsigned int num_threads)
{
    for (unsigned int i = 0; i < num_threads; i++)
    {
        CRef<TThread> thr(NewThread());
        thr->CountSelf();
        thr->Run();
    }
}
예제 #16
0
파일: thread.c 프로젝트: ayang64/aolserver
void
NsThreadMain(void *arg)
{
    ThreadArg   *argPtr = arg;
    Thread      *thrPtr;

    thrPtr = NewThread(argPtr);
    ns_free(argPtr);
    (*thrPtr->proc) (thrPtr->arg);
}
예제 #17
0
// Start the download thread
void ViDownloadThreadStart(VI_INSTALL_DLG *d)
{
	// Validate arguments
	if (d == NULL)
	{
		return;
	}

	d->DownloadStarted = true;
	d->DownloadThread = NewThread(ViDownloadThread, d);
}
예제 #18
0
파일: thread.c 프로젝트: ayang64/aolserver
static Thread *
GetThread(void)
{
    Thread *thrPtr;

    thrPtr = Ns_TlsGet(&key);
    if (thrPtr == NULL) {
	thrPtr = NewThread(NULL);
    }
    return thrPtr;
}
예제 #19
0
static Thread *
GetThread(void)
{
    Thread *thrPtr;

    thrPtr = pthread_getspecific(key);
    if (thrPtr == NULL) {
    	thrPtr = NewThread();
	SetKey("NsGetTls", thrPtr);
    }
    return thrPtr;
}
예제 #20
0
DWORD CWorker::CThreadWormThread::ThreadMain (PVOID lpParameter) {
   CTrace::CEnter Enter(CTrace::Layer::CODINE, L"Worker::ThreadWormThread::ThreadMain(PVOID)");
   static const DWORD dwLifetime = 2000;
   CWorker *TheWorker = (CWorker*)lpParameter;

   if (!TheWorker->m_bExit) {
      Sleep(TheWorker->m_dwDelay);
      CThreadWormThread NewThread(lpParameter);
      NewThread.Resume();
      Sleep(dwLifetime);
   }
   return 0;
}
예제 #21
0
파일: Nat.c 프로젝트: falcon8823/utvpn
// 管理コマンド受付初期化
void NiInitAdminAccept(NAT *n)
{
	THREAD *t;
	// 引数チェック
	if (n == NULL)
	{
		return;
	}

	t = NewThread(NiListenThread, n);
	WaitThreadInit(t);
	n->AdminAcceptThread = t;
}
예제 #22
0
파일: Nat.c 프로젝트: benapetr/SoftEtherVPN
// Initialize receiving management command
void NiInitAdminAccept(NAT *n)
{
	THREAD *t;
	// Validate arguments
	if (n == NULL)
	{
		return;
	}

	t = NewThread(NiListenThread, n);
	WaitThreadInit(t);
	n->AdminAcceptThread = t;
}
예제 #23
0
// アダプタを開く (Pcap)
ETH *OpenEthPcap(char *name, bool local, bool tapmode, char *tapaddr)
{
	char errbuf[PCAP_ERRBUF_SIZE];
	ETH *e;
	pcap_t *p;
	CANCEL *c;

	// 引数チェック
	if (name == NULL || tapmode != false)
	{
		return NULL;
	}
	
	// エラーメッセージバッファの初期化
	errbuf[0] = 0;

	// キャプチャデバイスを開く
	p = pcap_open_live(name, 65535, (local == false), 1, errbuf);
	if(p==NULL)
	{
		return NULL;
	}

	// ノンブロックモードに設定
	// BSD系OSでは、BPFのselectが正常に動作しないのでブロックさせないとビジーループになる
	/*
	if(pcap_setnonblock(p, true, errbuf) == -1)
	{
		Debug("pcap_setnonblock:%s\n",errbuf);
		pcap_close(p);
		return NULL;
	}
	*/
	
	e = ZeroMalloc(sizeof(ETH));
	e->Name = CopyStr(name);
	e->Title = CopyStr(name);
	e->Queue = NewQueue();
	e->QueueSize = 0;
	e->Cancel = NewCancel();
	e->IfIndex = -1;
	e->Socket = pcap_get_selectable_fd(p);
	e->Pcap = p;
	
	e->CaptureThread = NewThread(PcapThread, e);
	WaitThreadInit(e->CaptureThread);

	return e;
}
예제 #24
0
// Open Ethernet adapter (Pcap)
ETH *OpenEthPcap(char *name, bool local, bool tapmode, char *tapaddr)
{
	char errbuf[PCAP_ERRBUF_SIZE];
	ETH *e;
	pcap_t *p;
	CANCEL *c;

	// Validate arguments
	if (name == NULL || tapmode != false)
	{
		return NULL;
	}
	
	// Initialize error message buffer
	errbuf[0] = 0;

	// Open capturing device
	p = pcap_open_live(name, 65535, (local == false), 1, errbuf);
	if(p==NULL)
	{
		return NULL;
	}

	// Set to non-block mode
	// (In old BSD OSs, 'select(2)' don't block normally for BPF device. To prevent busy loop)
	/*
	if(pcap_setnonblock(p, true, errbuf) == -1)
	{
		Debug("pcap_setnonblock:%s\n",errbuf);
		pcap_close(p);
		return NULL;
	}
	*/
	
	e = ZeroMalloc(sizeof(ETH));
	e->Name = CopyStr(name);
	e->Title = CopyStr(name);
	e->Queue = NewQueue();
	e->QueueSize = 0;
	e->Cancel = NewCancel();
	e->IfIndex = -1;
	e->Socket = pcap_get_selectable_fd(p);
	e->Pcap = p;
	
	e->CaptureThread = NewThread(PcapThread, e);
	WaitThreadInit(e->CaptureThread);

	return e;
}
예제 #25
0
int ThreadCreate(ThreadData* aThreadData, void* aThreadId)
	{
	HarnessThread threadId;
	int retval=0;
	if(aThreadData->iSelf != EThreadMain)
		{
		retval = KNoPermission;
		}
	else
		{
		threadId = (HarnessThread)(int) aThreadId;
		retval = NewThread(aThreadData,threadId);
		}
	return retval;
	}
예제 #26
0
Boolean
SpawnSDThread(ThreadEntryProcPtr threadProc, ThreadID *tid)
{
	OSErr			err;
	
	err = NewThread(kCooperativeThread, (ThreadEntryProcPtr)threadProc, (void*) nil, 
					0, kCreateIfNeeded, (void**)nil, tid);
				//  ^---- 0 means gimme the default stack size from Thread Manager
	if (err == noErr)
		YieldToThread(*tid); /* force ctx switch */
	else
	{
		return false;
	}
	
	return true;
}
예제 #27
0
void ServerSocketListen(AbstractServerSocket abstractSocket, void ( onConnect)(void *)) {
	abstractSocket->address_size = sizeof abstractSocket->pin;
	while (1) {
		abstractSocket->temp_sock_descriptor = accept(
				abstractSocket->sock_descriptor,
				(struct sockaddr *) &(abstractSocket->pin),
				&(abstractSocket->address_size));

		if (abstractSocket->temp_sock_descriptor == -1) {
			perror("llamada para aceptar");
			exit(1);
		}
		printf("Conexion aceptada ... \n");
		AbstractThread threadConexion = NewThread();
		runWithParam(threadConexion, onConnect,
				(void *) NewClientConnection(abstractSocket->temp_sock_descriptor));
	}
}
예제 #28
0
 TestingSetup() {
     fPrintToDebugger = true; // don't want to write to debug.log file
     noui_connect();
     bitdb.MakeMock();
     pathTemp = GetTempPath() / strprintf("test_bytecoin_%lu_%i", (unsigned long)GetTime(), (int)(GetRand(100000)));
     boost::filesystem::create_directories(pathTemp);
     mapArgs["-datadir"] = pathTemp.string();
     pblocktree = new CBlockTreeDB(1 << 20, true);
     pcoinsdbview = new CCoinsViewDB(1 << 23, true);
     pcoinsTip = new CCoinsViewCache(*pcoinsdbview);
     InitBlockIndex();
     bool fFirstRun;
     pwalletMain = new CWallet("wallet.dat");
     pwalletMain->LoadWallet(fFirstRun);
     RegisterWallet(pwalletMain);
     nScriptCheckThreads = 3;
     for (int i=0; i < nScriptCheckThreads-1; i++)
         NewThread(ThreadScriptCheck, NULL);
 }
예제 #29
0
// Initialization of the Tick64
void InitTick64()
{
	if (tk64 != NULL)
	{
		// Already initialized
		return;
	}

	halt_tick_event = NewEvent();

	// Initialize the structure
	tk64 = ZeroMalloc(sizeof(TICK64));
	tk64->TickLock = NewLock();
	tk64->AdjustTime = NewList(NULL);

	// Creating a thread
	tk64->Thread = NewThread(Tick64Thread, NULL);
	WaitThreadInit(tk64->Thread);
}
예제 #30
0
// Creating a DDNS client
DDNS_CLIENT *NewDDNSClient(CEDAR *cedar, UCHAR *key, INTERNET_SETTING *t)
{
	DDNS_CLIENT *c;
	UCHAR key_hash[SHA1_SIZE];
	// Validate arguments
	if (cedar == NULL)
	{
		return NULL;
	}

	c = ZeroMalloc(sizeof(DDNS_CLIENT));
	c->Cedar = cedar;
	AddRef(c->Cedar->ref);

	c->Err_IPv4 = c->Err_IPv6 = ERR_TRYING_TO_CONNECT;

	if (key == NULL)
	{
		// Create a new key
		DCGenNewKey(c->Key);
	}
	else
	{
		// Set the key
		Copy(c->Key, key, SHA1_SIZE);
	}

	HashSha1(key_hash, c->Key, sizeof(c->Key));


	if (t != NULL)
	{
		Copy(&c->InternetSetting, t, sizeof(INTERNET_SETTING));
	}

	c->Lock = NewLock();

	// Thread creation
	c->Event = NewEvent();
	c->Thread = NewThread(DCThread, c);

	return c;
}