VirtualBox

source: vbox/trunk/include/iprt/queueatomic.h@ 66086

Last change on this file since 66086 was 62473, checked in by vboxsync, 8 years ago

(C) 2016

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 3.4 KB
Line 
1/** @file
2 * IPRT - Generic Work Queue with concurrent atomic access.
3 */
4
5/*
6 * Copyright (C) 2013-2016 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_queueatomic_h
27#define ___iprt_queueatomic_h
28
29#include <iprt/types.h>
30#include <iprt/asm.h>
31
32/** @defgroup grp_rt_queueatomic RTQueueAtomic - 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 RTQUEUEATOMICITEM
45{
46 /** Pointer to the next work item in the list. */
47 struct RTQUEUEATOMICITEM * volatile pNext;
48} RTQUEUEATOMICITEM;
49/** Pointer to a work item. */
50typedef RTQUEUEATOMICITEM *PRTQUEUEATOMICITEM;
51/** Pointer to a work item pointer. */
52typedef PRTQUEUEATOMICITEM *PPRTQUEUEATOMICITEM;
53
54/**
55 * Work queue.
56 */
57typedef struct RTQUEUEATOMIC
58{
59 /* Head of the work queue. */
60 volatile PRTQUEUEATOMICITEM pHead;
61} RTQUEUEATOMIC;
62/** Pointer to a work queue. */
63typedef RTQUEUEATOMIC *PRTQUEUEATOMIC;
64
65/**
66 * Initialize a work queue.
67 *
68 * @param pWorkQueue Pointer to an unitialised work queue.
69 */
70DECLINLINE(void) RTQueueAtomicInit(PRTQUEUEATOMIC 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) RTQueueAtomicInsert(PRTQUEUEATOMIC pWorkQueue, PRTQUEUEATOMICITEM pItem)
82{
83 PRTQUEUEATOMICITEM pNext = ASMAtomicUoReadPtrT(&pWorkQueue->pHead, PRTQUEUEATOMICITEM);
84 PRTQUEUEATOMICITEM 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(PRTQUEUEATOMICITEM) RTQueueAtomicRemoveAll(PRTQUEUEATOMIC pWorkQueue)
102{
103 PRTQUEUEATOMICITEM pHead = ASMAtomicXchgPtrT(&pWorkQueue->pHead, NULL, PRTQUEUEATOMICITEM);
104
105 /* Reverse it. */
106 PRTQUEUEATOMICITEM pCur = pHead;
107 pHead = NULL;
108 while (pCur)
109 {
110 PRTQUEUEATOMICITEM 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
124
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