| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 |
- /**
- * @file BPending.c
- * @author Ambroz Bizjak <ambrop7@gmail.com>
- *
- * @section LICENSE
- *
- * This file is part of BadVPN.
- *
- * BadVPN is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License version 2
- * as published by the Free Software Foundation.
- *
- * BadVPN 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 General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write to the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
- */
- #include <stddef.h>
- #include <misc/debug.h>
- #include <misc/offset.h>
- #include <system/BPending.h>
- void BPendingGroup_Init (BPendingGroup *g)
- {
- // init jobs list
- LinkedList1_Init(&g->jobs);
-
- // init pending counter
- DebugCounter_Init(&g->pending_ctr);
-
- // init debug object
- DebugObject_Init(&g->d_obj);
- }
- void BPendingGroup_Free (BPendingGroup *g)
- {
- DebugCounter_Free(&g->pending_ctr);
- ASSERT(LinkedList1_IsEmpty(&g->jobs))
- DebugObject_Free(&g->d_obj);
- }
- int BPendingGroup_HasJobs (BPendingGroup *g)
- {
- DebugObject_Access(&g->d_obj);
-
- return !LinkedList1_IsEmpty(&g->jobs);
- }
- void BPendingGroup_ExecuteJob (BPendingGroup *g)
- {
- ASSERT(!LinkedList1_IsEmpty(&g->jobs))
- DebugObject_Access(&g->d_obj);
-
- // get a job
- LinkedList1Node *node = LinkedList1_GetLast(&g->jobs);
- BPending *p = UPPER_OBJECT(node, BPending, pending_node);
- ASSERT(p->pending)
-
- // remove from jobs list
- LinkedList1_Remove(&g->jobs, &p->pending_node);
-
- // set not pending
- p->pending = 0;
-
- // execute job
- p->handler(p->user);
- return;
- }
- BPending * BPendingGroup_PeekJob (BPendingGroup *g)
- {
- DebugObject_Access(&g->d_obj);
-
- LinkedList1Node *node = LinkedList1_GetLast(&g->jobs);
- if (!node) {
- return NULL;
- }
-
- return UPPER_OBJECT(node, BPending, pending_node);
- }
- void BPending_Init (BPending *o, BPendingGroup *g, BPending_handler handler, void *user)
- {
- // init arguments
- o->g = g;
- o->handler = handler;
- o->user = user;
-
- // set not pending
- o->pending = 0;
-
- // increment pending counter
- DebugCounter_Increment(&o->g->pending_ctr);
-
- // init debug object
- DebugObject_Init(&o->d_obj);
- }
- void BPending_Free (BPending *o)
- {
- DebugCounter_Decrement(&o->g->pending_ctr);
- DebugObject_Free(&o->d_obj);
-
- // remove from jobs list
- if (o->pending) {
- LinkedList1_Remove(&o->g->jobs, &o->pending_node);
- }
- }
- void BPending_Set (BPending *o)
- {
- DebugObject_Access(&o->d_obj);
-
- // remove from jobs list
- if (o->pending) {
- LinkedList1_Remove(&o->g->jobs, &o->pending_node);
- }
-
- // insert to jobs list
- LinkedList1_Append(&o->g->jobs, &o->pending_node);
-
- // set pending
- o->pending = 1;
- }
- void BPending_Unset (BPending *o)
- {
- DebugObject_Access(&o->d_obj);
-
- if (o->pending) {
- // remove from jobs list
- LinkedList1_Remove(&o->g->jobs, &o->pending_node);
-
- // set not pending
- o->pending = 0;
- }
- }
- int BPending_IsSet (BPending *o)
- {
- DebugObject_Access(&o->d_obj);
-
- return o->pending;
- }
|