TEST(CLConditionVariable, Multi_process_for_Shared_Cond)
{
	CLSharedMemory *psm = new CLSharedMemory("test_for_multi_process_for_shared_cond", 16);
	long *p = (long *)(psm->GetAddress());
	*p = 0;

	long *flag = (long *)(((char *)p) + 8);
	*flag = 0;

	CLEvent event("test_for_event_auto");

	CLProcess *process = new CLProcess(new CLProcessFunctionForExec);
	EXPECT_TRUE(process->Run((void *)"../test_for_exec/test_for_CLConditionVariable/main").IsSuccess());

	CLMutex mutex("mutex_for_test_for_multi_process_for_shared_cond", MUTEX_USE_SHARED_PTHREAD);

	CLConditionVariable cv("test_conditoin_variable_for_multiprocess");

	{
		CLCriticalSection cs(&mutex);

		while(*flag == 0)
			EXPECT_TRUE((cv.Wait(&mutex)).IsSuccess());
	}

	EXPECT_EQ(*p, 5);

	EXPECT_TRUE(event.Wait().IsSuccess());

	delete psm;
}
Пример #2
0
int main(int argc, char* argv[])
{
	if(!CLLibExecutiveInitializer::Initialize().IsSuccess())
	{
		cout << "in test initialize error" << endl;
		return 0;
	}

	try
	{
		CLSharedMemory *psm = new CLSharedMemory("test_for_CLMutex_SharedMutexByPthread", 8);
		long *pg = (long *)(psm->GetAddress());

		pthread_t tid;
		pthread_create(&tid, 0, thread_for_shared_mutex_bypthread, pg);

		thread_for_shared_mutex_bypthread(pg);

		pthread_join(tid, 0);

		delete psm;

		throw CLStatus(0, 0);
	}
	catch(CLStatus& s)
	{
		{
			CLEvent event("test_for_event_auto");
			event.Set();
		}

		if(!CLLibExecutiveInitializer::Destroy().IsSuccess())
			cout << "in test destroy error" << endl;
	}

    return 0;
}
TEST(CLSharedConditionVariableAllocator, MultiProcess)
{
	const char *name_cv = "test_conditoin_variable_for_CLSharedConditionVariableAllocator_MultiProcess";
	const char *name_mutex = "mutex_for_test_for_CLSharedConditionVariableAllocator_MultiProcess";

	CLSharedMemory *psm = new CLSharedMemory("test_for_CLSharedConditionVariableAllocator_MultiProcess", 16);
	long *p = (long *)(psm->GetAddress());
	*p = 0;

	long *flag = (long *)(((char *)p) + 8);
	*flag = 0;

	pid_t pid = fork();
	if(pid == 0)
	{
		sleep(2);

		{
			CLMutex mutex(name_mutex, MUTEX_USE_SHARED_PTHREAD);

			{
				CLCriticalSection cs(&mutex);

				*flag = 1;

				*p = 5;
			}

			pthread_cond_t *pCV = CLSharedConditionVariableAllocator::Get(name_cv);

			CLConditionVariable cv(pCV);

			EXPECT_TRUE((cv.Wakeup()).IsSuccess());

			EXPECT_TRUE(CLSharedConditionVariableAllocator::Release(name_cv).IsSuccess());
		}

		delete psm;

		CLLibExecutiveInitializer::Destroy();

		_exit(0);
	}

	CLMutex mutex(name_mutex, MUTEX_USE_SHARED_PTHREAD);

	pthread_cond_t *pCV = CLSharedConditionVariableAllocator::Get(name_cv);

	CLConditionVariable cv(pCV);

	{
		CLCriticalSection cs(&mutex);

		while(*flag == 0)
			EXPECT_TRUE((cv.Wait(&mutex)).IsSuccess());
	}

	EXPECT_TRUE(CLSharedConditionVariableAllocator::Release(name_cv).IsSuccess());

	EXPECT_EQ(*p, 5);

	waitpid(pid, 0, 0);

	delete psm;
}