VirtualBox

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

Last change on this file since 77807 was 76585, checked in by vboxsync, 6 years ago

*: scm --fix-header-guard-endif

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 3.4 KB
RevLine 
[45723]1/** @file
[46185]2 * IPRT - Generic Work Queue with concurrent atomic access.
[45723]3 */
4
5/*
[76553]6 * Copyright (C) 2013-2019 Oracle Corporation
[45723]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
[76557]26#ifndef IPRT_INCLUDED_queueatomic_h
27#define IPRT_INCLUDED_queueatomic_h
[76507]28#ifndef RT_WITHOUT_PRAGMA_ONCE
29# pragma once
30#endif
[45723]31
32#include <iprt/types.h>
33#include <iprt/asm.h>
34
[53615]35/** @defgroup grp_rt_queueatomic RTQueueAtomic - Generic Work Queue
[45723]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 */
[46185]47typedef struct RTQUEUEATOMICITEM
[45723]48{
49 /** Pointer to the next work item in the list. */
[46185]50 struct RTQUEUEATOMICITEM * volatile pNext;
51} RTQUEUEATOMICITEM;
[45723]52/** Pointer to a work item. */
[46185]53typedef RTQUEUEATOMICITEM *PRTQUEUEATOMICITEM;
[45723]54/** Pointer to a work item pointer. */
[46185]55typedef PRTQUEUEATOMICITEM *PPRTQUEUEATOMICITEM;
[45723]56
57/**
58 * Work queue.
59 */
[46185]60typedef struct RTQUEUEATOMIC
[45723]61{
62 /* Head of the work queue. */
[46185]63 volatile PRTQUEUEATOMICITEM pHead;
64} RTQUEUEATOMIC;
[45723]65/** Pointer to a work queue. */
[46185]66typedef RTQUEUEATOMIC *PRTQUEUEATOMIC;
[45723]67
68/**
69 * Initialize a work queue.
70 *
71 * @param pWorkQueue Pointer to an unitialised work queue.
72 */
[46185]73DECLINLINE(void) RTQueueAtomicInit(PRTQUEUEATOMIC pWorkQueue)
[45723]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 */
[46185]84DECLINLINE(void) RTQueueAtomicInsert(PRTQUEUEATOMIC pWorkQueue, PRTQUEUEATOMICITEM pItem)
[45723]85{
[46185]86 PRTQUEUEATOMICITEM pNext = ASMAtomicUoReadPtrT(&pWorkQueue->pHead, PRTQUEUEATOMICITEM);
87 PRTQUEUEATOMICITEM pHeadOld;
[45723]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 */
[46185]104DECLINLINE(PRTQUEUEATOMICITEM) RTQueueAtomicRemoveAll(PRTQUEUEATOMIC pWorkQueue)
[45723]105{
[46185]106 PRTQUEUEATOMICITEM pHead = ASMAtomicXchgPtrT(&pWorkQueue->pHead, NULL, PRTQUEUEATOMICITEM);
[45723]107
108 /* Reverse it. */
[46185]109 PRTQUEUEATOMICITEM pCur = pHead;
[45723]110 pHead = NULL;
111 while (pCur)
112 {
[46185]113 PRTQUEUEATOMICITEM pInsert = pCur;
[45723]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
[76585]126#endif /* !IPRT_INCLUDED_queueatomic_h */
[53615]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