I'm writing a server application, task oriented. Each Session (client) send packets to the server, the server enqueues them as Tasks, and workers (threads) handle them. Constraints are :
- Tasks from a same Session have to be always sequentially executed ( => if a thread begin to handle a Task of a Session, another thread cannot handle a Task for the same Session while the first is not finished)
- Tasks of some types have to be sequentially executed
- Tasks of some other type don't have to be sequentially executed
What could be the best approach to do that ? how do we call this concept ? (I guess it is not double conditional-sequence execution)
I ideally would use TBB to achieve that but I don't know if a feature of TBB just fit for my need. (I'm open for any other propositions)
Here a piece of "pseudo-code" which would be my first idea [EDIT: but is wrong, sequential execution is not assured, but the code could explain my idea] :
struct Session {
atomic<bool> locked;
}
struct Task {
char type;
Session* session;
void execute() { ; }
};
atomic<bool> type_locked[SEQ_TYPE_NUMBER];
threadsafe_selfiterable_list<Task> list;
void do() {
while( alive )
{
if( list.empty() )
relax();
if( list.has_next() ) {
task = list.next();
if( !compare_and_swap( task.session->locked, true ) ) {
if( is_non_sequential_task_type( task.type ) ) {
queue.pop();
task.execute();
} else if( !compare_and_swap( type_locked[task.type], true ) ) {
queue.pop();
task.execute();
type_locked[task.type] = false;
}
task.session->locked = false;
}
} else
list.rewind();
}
}