File: Task.cc

package info (click to toggle)
metview 5.3.0-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 44,932 kB
  • sloc: cpp: 211,811; ansic: 33,467; xml: 3,942; sh: 2,942; yacc: 1,183; fortran: 762; lex: 761; perl: 700; python: 340; f90: 236; makefile: 92
file content (142 lines) | stat: -rw-r--r-- 2,687 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/***************************** LICENSE START ***********************************

 Copyright 2012 ECMWF and INPE. This software is distributed under the terms
 of the Apache License version 2.0. In applying this license, ECMWF does not
 waive the privileges and immunities granted to it by virtue of its status as
 an Intergovernmental Organization or submit itself to any jurisdiction.

 ***************************** LICENSE END *************************************/

#include "Task.h"

#ifdef PM_USE_THREADS
#include "Mutex.hpp"
#endif

// -- Lists for holding tasks
//    pendingTaskList: holds the tasks to be processed first
//    cleanupTaskList: holds the taks to be processed in the end

PendingTaskList TaskBase::pendingTaskList_;
PendingTaskList TaskBase::cleanupTaskList_;

TaskBase::TaskBase()
{
	// Empty
}

TaskBase::~TaskBase()
{
	// Empty
}

void
TaskBase::RegisterTask ( TaskBase* task )
{
#ifdef PM_USE_THREADS
	glMutex.Lock();
#endif
	pendingTaskList_.push_back ( task );

#ifdef PM_USE_THREADS
	glMutex.Unlock();
#endif
}

// Register a task on the cleanup list
void 
TaskBase::RegisterCleanUpTask ( TaskBase*  task )
{
	cleanupTaskList_.push_back ( task );
}

void*
TaskBase::Run(void *)
{
	PendingTaskList:: iterator t;

	for (  t = pendingTaskList_.begin();
		t != pendingTaskList_.end(); ++t )
	{
		// Execute the tasks
		(*t)->Execute();

		// delete the task element
		TaskBase *pt =  *t;
		delete pt;

		// make the element of the list point to zero
	        (*t) = 0;
	}

	return 0;
}

void
TaskBase::Flush()
{
	TaskBase::FlushPending();
	TaskBase::FlushCleanUp();
}

// -- Flush Pending
//    execute all pending tasks (called first)
void 
TaskBase::FlushPending()
{

#ifdef PM_USE_THREADS
	pthread_attr_t attr;
	pthread_attr_init(&attr);
	pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
	pthread_t thread;
	pthread_create(&thread,&attr,TaskBase::Run,0);
	pthread_attr_destroy(&attr);
#else

	PendingTaskList:: iterator t;

	for (  t = pendingTaskList_.begin();
		t != pendingTaskList_.end(); ++t )
	{
		// Execute the tasks
		(*t)->Execute();

		// delete the task element
		TaskBase *pt =  *t;
		delete pt;

		// make the element of the list point to zero
	        (*t) = 0;

	}

	pendingTaskList_.clear();
#endif

}

// -- Flush CleanUp
//
// Execute all clean up tasks (called in the end)
void 
TaskBase::FlushCleanUp()
{
	PendingTaskList::iterator t;

	for (  t = cleanupTaskList_.begin();
		t != cleanupTaskList_.end(); ++t )
	{
		// Execute the tasks
		(*t)->Execute();

		// delete the task element
		TaskBase *pt =  *t;
		delete pt;

		// make the element of the list point to zero
	        (*t) = 0;
	}

	cleanupTaskList_.clear();
}