VirtualBox

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

Last change on this file since 86577 was 82968, checked in by vboxsync, 5 years ago

Copyright year updates by scm.

  • 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-2020 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_INCLUDED_queueatomic_h
27#define IPRT_INCLUDED_queueatomic_h
28#ifndef RT_WITHOUT_PRAGMA_ONCE
29# pragma once
30#endif
31
32#include <iprt/types.h>
33#include <iprt/asm.h>
34
35/** @defgroup grp_rt_queueatomic RTQueueAtomic - Generic Work Queue
36 * @ingroup grp_rt
37 *
38 * Implementation of a lockless work queue for threaded environments.
39 * @{
40 */
41
42RT_C_DECLS_BEGIN
43
44/**
45 * A work item
46 */
47typedef struct RTQUEUEATOMICITEM
48{
49 /** Pointer to the next work item in the list. */
50 struct RTQUEUEATOMICITEM * volatile pNext;
51} RTQUEUEATOMICITEM;
52/** Pointer to a work item. */
53typedef RTQUEUEATOMICITEM *PRTQUEUEATOMICITEM;
54/** Pointer to a work item pointer. */
55typedef PRTQUEUEATOMICITEM *PPRTQUEUEATOMICITEM;
56
57/**
58 * Work queue.
59 */
60typedef struct RTQUEUEATOMIC
61{
62 /* Head of the work queue. */
63 volatile PRTQUEUEATOMICITEM pHead;
64} RTQUEUEATOMIC;
65/** Pointer to a work queue. */
66typedef RTQUEUEATOMIC *PRTQUEUEATOMIC;
67
68/**
69 * Initialize a work queue.
70 *
71 * @param pWorkQueue Pointer to an unitialised work queue.
72 */
73DECLINLINE(void) RTQueueAtomicInit(PRTQUEUEATOMIC pWorkQueue)
74{
75 ASMAtomicWriteNullPtr(&pWorkQueue->pHead);
76}
77
78/**
79 * Insert a new item into the work queue.
80 *
81 * @param pWorkQueue The work queue to insert into.
82 * @param pItem The item to insert.
83 */
84DECLINLINE(void) RTQueueAtomicInsert(PRTQUEUEATOMIC pWorkQueue, PRTQUEUEATOMICITEM pItem)
85{
86 PRTQUEUEATOMICITEM pNext = ASMAtomicUoReadPtrT(&pWorkQueue->pHead, PRTQUEUEATOMICITEM);
87 PRTQUEUEATOMICITEM pHeadOld;
88 pItem->pNext = pNext;
89 while (!ASMAtomicCmpXchgExPtr(&pWorkQueue->pHead, pItem, pNext, &pHeadOld))
90 {
91 pNext = pHeadOld;
92 Assert(pNext != pItem);
93 pItem->pNext = pNext;
94 ASMNopPause();
95 }
96}
97
98/**
99 * Remove all items from the given work queue and return them in the inserted order.
100 *
101 * @returns Pointer to the first item.
102 * @param pWorkQueue The work queue.
103 */
104DECLINLINE(PRTQUEUEATOMICITEM) RTQueueAtomicRemoveAll(PRTQUEUEATOMIC pWorkQueue)
105{
106 PRTQUEUEATOMICITEM pHead = ASMAtomicXchgPtrT(&pWorkQueue->pHead, NULL, PRTQUEUEATOMICITEM);
107
108 /* Reverse it. */
109 PRTQUEUEATOMICITEM pCur = pHead;
110 pHead = NULL;
111 while (pCur)
112 {
113 PRTQUEUEATOMICITEM pInsert = pCur;
114 pCur = pCur->pNext;
115 pInsert->pNext = pHead;
116 pHead = pInsert;
117 }
118
119 return pHead;
120}
121
122RT_C_DECLS_END
123
124/** @} */
125
126#endif /* !IPRT_INCLUDED_queueatomic_h */
127
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