Sponsored Content
Special Forums UNIX and Linux Applications High Performance Computing Memory Barriers for (Ubuntu) Linux (i686) Post 302429949 by gorga on Wednesday 16th of June 2010 08:20:50 AM
Old 06-16-2010
Memory Barriers for (Ubuntu) Linux (i686)

Hi all,

(Hope this is the right forum for this question)

I have some multi-threaded C code (compiled with GCC 4.4.3) which accesses shared variables. Although I've marked those variables with volatile to guard against compiler re-ordering, I'm concerned that processor out-of-order execution may cause my code to fail, and I'm looking for a "low-cost" method of guaranteeing ordering is maintained in my code.

For example, I have something like...

Code:
memset(&task, 0, sizeof(task_t));/* null memory */
task.id.prefix = prefix_id;
task.id.instance = instance_id;
/* write-memorybarrier required here */
task.state = task_ready;

Where I need to ensure that the "task state" is only set to "task_ready" after the previous instructions have been committed. As the "task" is shared between threads, another thread seeing the state as "ready" may try to access its member variables, so it's vital that the tasks "prefix" and "instance" have been updated.

I know this is a common problem and mutexes and semaphores provide in-built memory barriers to address this problem but I'm trying to build a scalable application and I want to avoid their use if possible. I also know GCC provides built-in atomic operations but I see they involve locking the data-bus, and I've heard about system primitives like "smp_wmb()" but I'm not sure how to incorporate these into my "user-space" program as they are platform dependent.

Therefore can anyone provide pointers or advise on how best (in terms of scalability and speed) to guarantee ordering is maintained?

Thanks.
 

4 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Memory-waste in Ubuntu/Debian?

I have 512 mem on this laptop, though 'top' tells me I only have 380. However, Ubuntu is using 288 mb of memory, when I only have 3 terminals, running lynx, vim(for this file) and (of course) top. Considering it I have lynx running a 600 page txt file, which of course would eat some memory but 300?... (0 Replies)
Discussion started by: riwa
0 Replies

2. Linux

i686, x86 64, ppc

Hi, i am quite new to linux. I am interested in fedora linux distro. Fedora Project I dont know which one to choose, either i686, x86 64 or ppc. I prefer a live cd, coz its easy to use. And what is the difference between "Fedora Desktop Live Media" and "Fedora KDE Live Media". (3 Replies)
Discussion started by: superblacksmith
3 Replies

3. Programming

Getting the total virtual memory for ubuntu in c++

Hi guys , i need to get the total virtual memory in ubuntu but i need to write a C++ code for that, any idea on how to go about doing it? any references? or website that i can refer to ? (6 Replies)
Discussion started by: xiaojesus
6 Replies

4. Ubuntu

XP and Linux (Ubuntu) on same disk, Can I install Ubuntu on not-yet partitioned portion of disk?

My PC (Esprimo, 3 yeas old) has one hard drive having 2 partitions C: (80 GB NTFS, XP) and D: (120 GB NTFS, empty) and and a 200 MB area that yet is not-partitioned. I would like to try Ubuntu and to install Ubuntu on the not-partitioned area . The idea is to have the possibility to run... (7 Replies)
Discussion started by: C.Weidemann
7 Replies
taskq(9F)																 taskq(9F)

NAME
taskq, ddi_taskq_create, ddi_taskq_destroy, ddi_taskq_dispatch, ddi_taskq_wait, ddi_taskq_suspend, taskq_suspended, ddi_taskq_resume - Ker- nel task queue operations SYNOPSIS
#include <sys/sunddi.h> ddi_taskq_t *ddi_taskq_create(dev_info_t *dip, const char *name, int nthreads, pri_t pri, uint_t cflags); void ddi_taskq_destroy(ddi_taskq_t *tq); int ddi_taskq_dispatch(ddi_taskq_t *tq, void (* func)(void *), void *arg, uint_t dflags); void ddi_taskq_wait(ddi_taskq_t *tq); void ddi_taskq_suspend(ddi_taskq_t *tq); boolean_t ddi_taskq_suspended(ddi_taskq_t *tq); void ddi_taskq_resume(ddi_taskq_t *tq); INTERFACE LEVEL
Solaris DDI specific (Solaris DDI) PARAMETERS
dip Pointer to the device's dev_info structure. May be NULL for kernel modules that do not have an associated dev_info struc- ture. name Descriptive string. Only alphanumeric characters can be used in name and spaces are not allowed. The name should be unique. nthreads Number of threads servicing the task queue. Note that the request ordering is guaranteed (tasks are processed in the order scheduled) if the taskq is created with a single servicing thread. pri Priority of threads servicing the task queue. Drivers and modules should specify TASKQ_DEFAULTPRI. cflags Should pass 0 as flags. func Callback function to call. arg Argument to the callback function. dflags Possible dflags are: DDI_SLEEP Allow sleeping (blocking) until memory is available. DDI_NOSLEEP Return DDI_FAILURE immediately if memory is not available. tq Pointer to a task queue (ddi_taskq_t *). tp Pointer to a thread structure. A kernel task queue is a mechanism for general-purpose asynchronous task scheduling that enables tasks to be performed at a later time by another thread. There are several reasons why you may utilize asynchronous task scheduling: 1. You have a task that isn't time-critical, but a current code path that is. 2. You have a task that may require grabbing locks that a thread already holds. 3. You have a task that needs to block (for example, to wait for memory), but a have a thread that cannot block in its current context. 4. You have a code path that can't complete because of a specific condition, but also can't sleep or fail. In this case, the task is imme- diately queued and then is executed after the condition disappears. 5. A task queue is just a simple way to launch multiple tasks in parallel. A task queue consists of a list of tasks, together with one or more threads to service the list. If a task queue has a single service thread, all tasks are guaranteed to execute in the order they were dispatched. Otherwise they can be executed in any order. Note that since tasks are placed on a list, execution of one task and should not depend on the execution of another task or a deadlock may occur. A taskq created with a single servicing thread guarantees that all the tasks are serviced in the order in which they are scheduled. The ddi_taskq_create() function creates a task queue instance. The ddi_taskq_dispatch() function places taskq on the list for later execution. The dflag argument specifies whether it is allowed sleep waiting for memory. DDI_SLEEP dispatches can sleep and are guaranteed to succeed. DDI_NOSLEEP dispatches are guaranteed not to sleep but may fail (return DDI_FAILURE) if resources are not available. The ddi_taskq_destroy() function waits for any scheduled tasks to complete, then destroys the taskq. The caller should guarantee that no new tasks are scheduled for the closing taskq. The ddi_taskq_wait() function waits for all previously scheduled tasks to complete. Note that this function does not stop any new task dis- patches. The ddi_taskq_suspend() function suspends all task execution until ddi_taskq_resume() is called. Although ddi_taskq_suspend() attempts to suspend pending tasks, there are no guarantees that they will be suspended. The only guarantee is that all tasks dispatched after ddi_taskq_suspend() will not be executed. Because it will trigger a deadlock, the ddi_taskq_suspend() function should never be called by a task executing on a taskq. The ddi_taskq_suspended() function returns B_TRUE if taskq is suspended, and B_FALSE otherwise. It is intended to ASSERT that the task queue is suspended. The ddi_taskq_resume() function resumes task queue execution. RETURN VALUES
The ddi_taskq_create() function creates an opaque handle that is used for all other taskq operations. It returns a taskq pointer on success and NULL on failure. The ddi_taskq_dispatch() function returns DDI_FAILURE if it can't dispatch a task and returns DDI_SUCCESS if dispatch succeeded. The ddi_taskq_suspended() function returns B_TRUE if taskq is suspended. Otherwise B_FALSE is returned. CONTEXT
All functions may be called from the user or kernel contexts. Addtionally, the ddi_taskq_dispatch function may be called from the interrupt context only if the DDI_NOSLEEP flag is set. 1 Mar 2005 taskq(9F)
All times are GMT -4. The time now is 03:25 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy