gcc: __attribute__ ((weak))

While playing with glibc I noticed one interesting technique. It is
based on gcc's extension that allows you to declare weak symbols. In
essence it works like this:


extern void weak_f (void) __attribute__ ((weak));

int main ()
{
  if (weak_f)
  {
    weak_f ();
  }
}


When you link such code ld won't complain if weak_f is unresolved. 
Plus you can check at run-time if symbol has been resolved as shown 
above.

Interesting effects can be achieved using this technique. For example
you can make some code thread-safe on-demand. You compile this code
once and depending on kind of application it is used in it automatically
becomes multi-threaded or single-threaded:

#include <pthread.h>

extern "C" int
pthread_create (pthread_t*, 
                const pthread_attr_t*, 
                void* (*)(void*), 
                void*) __attribute__ ((weak));

extern "C" int
pthread_mutex_init (pthread_mutex_t*, 
                    const pthread_mutexattr_t*) __attribute__ ((weak));

extern "C" int
pthread_mutex_lock (pthread_mutex_t*) __attribute__ ((weak));

extern "C" int
pthread_mutex_unlock (pthread_mutex_t*) __attribute__ ((weak));

extern "C" int
pthread_mutex_destroy (pthread_mutex_t*) __attribute__ ((weak));


class foo
{
public:

  foo ()
  {
    if (pthread_create) pthread_mutex_init (&m_, 0);
  }
  
  ~foo ()
  {
    if (pthread_create) pthread_mutex_destroy (&m_);
  }


  bar ()
  {
    if (pthread_create) pthread_mutex_lock (&m_);

    // ...

    if (pthread_create) pthread_mutex_unlock (&m_);
  }

private:
  pthread_mutex_t m_;

  // some other data that mutex protects
};


Now if you link your code with libpthread, foo is automatically 
multi-thread-safe. If you don't then all the calls to mutex API 
are skipped.

This could be especially nice if class foo is in the library that
is used by both kinds of applications.

---------------------------

Function Pointer Syntax

The syntax for declaring a function pointer might seem messy at first, but in most cases it's really quite straight-forward once you  understand  what's going on. Let's look at a simple example:
void (*foo)(int);
In this example, foo is a pointer to a function taking one argument, an integer, and that returns void. It's as if you're declaring a function called "*foo", which takes an int and returns void; now, if *foo is a function, then foo must be a pointer to a function. (Similarly, a declaration like int *x can be read as *x is an int, so x must be a pointer to an int.) 

The key to writing the declaration for a function pointer is that you're just writing out the declaration of a function but with (*func_name) where you'd normally just put func_name. 

Reading Function Pointer Declarations

Sometimes people get confused when more stars are thrown in:
void *(*foo)(int *);
Here, the key is to read inside-out; notice that the innermost element of the expression is *foo, and that otherwise it looks like a normal function declaration. *foo should refer to a function that returns a void * and takes an int *. Consequently, foo is a pointer to just such a function.

The argument expected by pthread_create is,

void *(*)(void *)

that is a pointer to function accepting a void pointer and returning a void pointer.





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值