VirtualBox

source: vbox/trunk/include/iprt/workqueue.h@ 45809

Last change on this file since 45809 was 45723, checked in by vboxsync, 12 years ago

Runtime: Async I/O manager for IPRT based on PDMAsyncCompletion* but simpler, will be used in testcases later (hacking in progress)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 3.2 KB
Line 
1/** @file
2 * IPRT - Generic Work Queue with concurrent access.
3 */
4
5/*
6 * Copyright (C) 2013 Oracle Corporation
7 *
8 * This file is part of VirtualBox Open Source Edition (OSE), as
9 * available from http://www.virtualbox.org. This file is free software;
10 * you can redistribute it and/or modify it under the terms of the GNU
11 * General Public License (GPL) as published by the Free Software
12 * Foundation, in version 2 as it comes in the "COPYING" file of the
13 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
14 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
15 *
16 * The contents of this file may alternatively be used under the terms
17 * of the Common Development and Distribution License Version 1.0
18 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
19 * VirtualBox OSE distribution, in which case the provisions of the
20 * CDDL are applicable instead of those of the GPL.
21 *
22 * You may elect to license modified versions of this file under the
23 * terms and conditions of either the GPL or the CDDL or both.
24 */
25
26#ifndef ___iprt_workqueue_h
27#define ___iprt_workqueue_h
28
29#include <iprt/types.h>
30#include <iprt/asm.h>
31
32/** @defgroup grp_rt_list RTWorkQueue - Generic Work Queue
33 * @ingroup grp_rt
34 *
35 * Implementation of a lockless work queue for threaded environments.
36 * @{
37 */
38
39RT_C_DECLS_BEGIN
40
41/**
42 * A work item
43 */
44typedef struct RTWORKITEM
45{
46 /** Pointer to the next work item in the list. */
47 struct RTWORKITEM * volatile pNext;
48} RTWORKITEM;
49/** Pointer to a work item. */
50typedef RTWORKITEM *PRTWORKITEM;
51/** Pointer to a work item pointer. */
52typedef PRTWORKITEM *PPRTWORKITEM;
53
54/**
55 * Work queue.
56 */
57typedef struct RTWORKQUEUE
58{
59 /* Head of the work queue. */
60 volatile PRTWORKITEM pHead;
61} RTWORKQUEUE;
62/** Pointer to a work queue. */
63typedef RTWORKQUEUE *PRTWORKQUEUE;
64
65/**
66 * Initialize a work queue.
67 *
68 * @param pWorkQueue Pointer to an unitialised work queue.
69 */
70DECLINLINE(void) RTWorkQueueInit(PRTWORKQUEUE pWorkQueue)
71{
72 ASMAtomicWriteNullPtr(&pWorkQueue->pHead);
73}
74
75/**
76 * Insert a new item into the work queue.
77 *
78 * @param pWorkQueue The work queue to insert into.
79 * @param pItem The item to insert.
80 */
81DECLINLINE(void) RTWorkQueueInsert(PRTWORKQUEUE pWorkQueue, PRTWORKITEM pItem)
82{
83 PRTWORKITEM pNext = ASMAtomicUoReadPtrT(&pWorkQueue->pHead, PRTWORKITEM);
84 PRTWORKITEM pHeadOld;
85 pItem->pNext = pNext;
86 while (!ASMAtomicCmpXchgExPtr(&pWorkQueue->pHead, pItem, pNext, &pHeadOld))
87 {
88 pNext = pHeadOld;
89 Assert(pNext != pItem);
90 pItem->pNext = pNext;
91 ASMNopPause();
92 }
93}
94
95/**
96 * Remove all items from the given work queue and return them in the inserted order.
97 *
98 * @returns Pointer to the first item.
99 * @param pWorkQueue The work queue.
100 */
101DECLINLINE(PRTWORKITEM) RTWorkQueueRemoveAll(PRTWORKQUEUE pWorkQueue)
102{
103 PRTWORKITEM pHead = ASMAtomicXchgPtrT(&pWorkQueue->pHead, NULL, PRTWORKITEM);
104
105 /* Reverse it. */
106 PRTWORKITEM pCur = pHead;
107 pHead = NULL;
108 while (pCur)
109 {
110 PRTWORKITEM pInsert = pCur;
111 pCur = pCur->pNext;
112 pInsert->pNext = pHead;
113 pHead = pInsert;
114 }
115
116 return pHead;
117}
118
119RT_C_DECLS_END
120
121/** @} */
122
123#endif
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette