Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Shared Memory (Semaphore and mutex)



On Oct 1, 2004, at 1:46 PM, Shaun Wexler wrote:

If you posted the entire code to your lock functions (ie what you need to do in the middle), maybe we could recommend something?



Let's take a simple case, I want to set a lock (based on the assembly in PowerPC book):


#include <stdio.h>
static inline void _lock_(volatile unsigned char *__count) {

register unsigned char _t; // so I don't have to worry about safe Regs

    __asm__ __volatile__
    (
    "\n"
    "_lock_1:\n"
    "   lwarx  %0,0,%1 \n"     // load lock and reserve
    "   addi   %0,0,1  \n"     // set lock (1 is locked)
    "   stwcx. %0,0,%1 \n"     // try to set lock
    "   bne-   _lock_1 \n"   // loop if lost reservation
    "   isync      \n"         // import barrier
    : "=&r" (_t)
    : "r"   (__count)
    : "cc", "memory"
    );
}

volatile unsigned char testB = 0;

int main(void) {

    printf("testB is %d\nNow locking.\n", testB);
    _lock_(&testB);
    printf("testB is now %d\n", testB);
     return 0;
}

gcc -O inline-test.c -o inline-test

I get:
testB is 0
Now locking.
testB is now 0

From the assembly (gcc -O inline-test.c -S), it seems that it is sending the address of testB, but it loaded r29 with the value and is sending that to printf the second time (if I'm reading this correctly) See below.

.data
_testB:
        .byte   0
.cstring
        .align 2
LC0:
        .ascii "testB is %d\12Now locking.\12\0"
        .align 2
LC1:
        .ascii "testB is now %d\12\0"
        .align 2

[snip]

        addis r29,r31,ha16(_testB-"L00000000001$pb")
        la r29,lo16(_testB-"L00000000001$pb")(r29)
        addis r3,r31,ha16(LC0-"L00000000001$pb")
        la r3,lo16(LC0-"L00000000001$pb")(r3)
        lbz r4,0(r29)
        bl L_printf$stub

        addis r2,r31,ha16(_testB-"L00000000001$pb")
        la r2,lo16(_testB-"L00000000001$pb")(r2)

_lock_1:
   lwarx  r0,0,r2
   addi   r0,0,1
   stwcx. r0,0,r2
   bne-   _lock_1
   isync

        addis r3,r31,ha16(LC1-"L00000000001$pb")
        la r3,lo16(LC1-"L00000000001$pb")(r3)
        lbz r4,0(r29)
        bl L_printf$stub


_______________________________________________ Do not post admin requests to the list. They will be ignored. PerfOptimization-dev mailing list (email@hidden) Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/perfoptimization-dev/email@hidden

This email sent to email@hidden
References: 
 >Re: Shared Memory (Semaphore and mutex) (From: Marc Colosimo <email@hidden>)
 >Re: Shared Memory (Semaphore and mutex) (From: Shaun Wexler <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.