EDN首页   博客首页

0

关于投票
关于c中volatile关键字(转贴)
一个定义为volatile的变量是说这变量可能会被意想不到地改变,这样,编译器就不会去假设这个变量的值了。精确地说就是,优化器在用到这个变量时必须每次都小心地重新读取这个变量的值,而不是使用保存在寄存器里的备份。下面是volatile变量的几个例子:
    1). 并行设备的硬件寄存器(如:状态寄存器)
    2). 一个中断服务子程序中会访问到的非自动变量(Non-automatic variables)
    3). 多线程应用中被几个任务共享的变量
    回答不出这个问题的人是不会被雇佣的。我认为这是区分C程序员和嵌入式系统程序员的最基本的问题。嵌入式系统程序员经常同硬件、中断、RTOS等等打交道,所用这些都要求volatile变量。不懂得volatile内容将会带来灾难。
    假设被面试者正确地回答了这是问题(嗯,怀疑这否会是这样),我将稍微深究一下,看一下这家伙是不是直正懂得volatile完全的重要性。
    1). 一个参数既可以是const还可以是volatile吗?解释为什么。
    2). 一个指针可以是volatile 吗?解释为什么。
    3). 下面的函数有什么错误:
         int square(volatile int *ptr)
         {
              return *ptr * *ptr;
         }
    下面是答案:
    1). 是的。一个例子是只读的状态寄存器。它是volatile因为它可能被意想不到地改变。它是const因为程序不应该试图去修改它。
    2). 是的。尽管这并不很常见。一个例子是当一个中服务子程序修该一个指向一个buffer的指针时。
    3). 这段代码的有个恶作剧。这段代码的目的是用来返指针*ptr指向值的平方,但是,由于*ptr指向一个volatile型参数,编译器将产生类似下面的代码:
    int square(volatile int *ptr)
    {
         int a,b;
         a = *ptr;
         b = *ptr;
         return a * b;
     }
    由于*ptr的值可能被意想不到地该变,因此a和b可能是不同的。结果,这段代码可能返不是你所期望的平方值!正确的代码如下:
     long square(volatile int *ptr)
     {
            int a;
            a = *ptr;
            return a * a;
     }
     
    
    
Volatile 关键字告诉编译器不要持有变量的临时性拷贝。一般用在多线程程序中,以避免在其中一个线程操作该变量时,将其拷贝入寄存器。请看以下情形:

   A线程将变量复制入寄存器,然后进入循环,反复检测寄存器的值是否满足一定条件(它期待B线程改变变量的值。
在此种情况下,当B线程改变了变量的值时,已改变的值对其在寄存器的值没有影响。所以A线程进入死循环。
  
   volatile 就是在此种情况下使用。
 

What does volatile do?

This is probably best explained by comparing the effects that volatile and synchronized have on a method. volatile is a field modifier, while synchronized modifies code blocks and methods. So we can specify three variations of a simple accessor using those two keywords:

         int i1;              int geti1() {return i1;}
volatile int i2;              int geti2() {return i2;}
         int i3; synchronized int geti3() {return i3;}

geti1() accesses the value currently stored in i1 in the current thread. Threads can have local copies of variables, and the data does not have to be the same as the data held in other threads. In particular, another thread may have updated i1 in it's thread, but the value in the current thread could be different from that updated value. In fact Java has the idea of a "main" memory, and this is the memory that holds the current "correct" value for variables. Threads can have their own copy of data for variables, and the thread copy can be different from the "main" memory. So in fact, it is possible for the "main" memory to have a value of 1 for i1, for thread1 to have a value of 2 for i1 and for thread2 to have a value of 3 for i1 if thread1 and thread2 have both updated i1 but those updated value has not yet been propagated to "main" memory or other threads.

On the other hand, geti2() effectively accesses the value of i2 from "main" memory. A volatile variable is not allowed to have a local copy of a variable that is different from the value currently held in "main" memory. Effectively, a variable declared volatile must have it's data synchronized across all threads, so that whenever you access or update the variable in any thread, all other threads immediately see the same value. Of course, it is likely that volatile variables have a higher access and update overhead than "plain" variables, since the reason threads can have their own copy of data is for better efficiency.

Well if volatile already synchronizes data across threads, what is synchronized for? Well there are two differences. Firstly synchronized obtains and releases locks on monitors which can force only one thread at a time to execute a code block, if both threads use the same monitor (effectively the same object lock). That's the fairly well known aspect to synchronized. But synchronized also synchronizes memory. In fact synchronized synchronizes the whole of thread memory with "main" memory. So executing geti3() does the following:

  1. The thread acquires the lock on the monitor for object this (assuming the monitor is unlocked, otherwise the thread waits until the monitor is unlocked).
  2. The thread memory flushes all its variables, i.e. it has all of its variables effectively read from "main" memory (JVMs can use dirty sets to optimize this so that only "dirty" variables are flushed, but conceptually this is the same. See section 17.9 of the Java language specification).
  3. The code block is executed (in this case setting the return value to the current value of i3, which may have just been reset from "main" memory).
  4. (Any changes to variables would normally now be written out to "main" memory, but for geti3() we have no changes.)
  5. The thread releases the lock on the monitor for object this.

So where volatile only synchronizes the value of one variable between thread memory and "main" memory, synchronized synchronizes the value of all variables between thread memory and "main" memory, and locks and releases a monitor to boot. Clearly synchronized is likely to have more overhead than volatile.

系统分类: 嵌入式
用户分类: 嵌入式开发
标签: c,volatile,嵌入式,编译器
来源: 转贴
发表评论 阅读全文(449) | 回复(0)

1

关于投票
ARM无痛苦起步(转)

ARM无痛苦起步(转) 

[ 2006-10-27 18:30:00 | By: huolf ]
 
      首先看看我们要解决的问题。44B0X片内只有几K CACHE,ROM和RAM都是外接的芯片。我们的程序是要写入FLASH中保存,但执行时是拷到SDRAM中执行的(如在ROM中执行速度会较慢)。要做到这一点需要把程序做成两个分程序:一个是实现你的系统功能的主程序,如果你用嵌入式系统,那就是UCOS和UCLINUX之类的程序,这个程序的代码保存在FLASH中,但执行时会拷到RAM中再执行;一个是引导程序,直接在FLASH中执行,负责把初始化芯片和外设,并把主程序从FLASH中拷到RAM中,然后跳到主程序去执行,对应的概念是UBOOT等常见的引导程序,这个程序会被写入0X0开始的地址,开机后自动执行。

    那么我们需要解决以下几个问题:
   1.如何编译和调试主程序
   2.如何使中断跳到RAM中的中断服务程序执行
   3.如何把引导程序和主程序写入FLASH中.

arm(44b0x)无痛苦起步

    这两个星期在看arm的资料,在朋友的帮助下弄明白了是怎么回事后,我觉得我原来看的入门资料总有些说的不详细的地方。所以决定写一个文档,总结开始arm开发的入门知识,给后来的朋友参考,也希望得到高手指正我认识上的错误。我的开发板是s3c44b0x的,2m NOR FLASH在bank0,8m sdram在bank6.
    首先看看我们要解决的问题。有些ARM芯片有内嵌的RAM 和FALSH.这样可以直接在片内运行程序,44B0X片内只有几K CACHE,ROM和RAM都是外接的芯片。我们的程序是要写入FLASH中保存,但执行时是拷到SDRAM中执行的(如在ROM中执行速度会较慢)。要做到这一点需要把程序做成两个分程序:一个是实现你的系统功能的主程序,如果你用嵌入式系统,那就是UCOS和UCLINUX之类的程序,这个程序的代码保存在FLASH中,但执行时会拷到RAM中再执行;一个是引导程序,直接在FLASH中执行,负责把初始化芯片和外设,并把主程序从FLASH中拷到RAM中,然后跳到主程序去执行,对应的概念是UBOOT等常见的引导程序,这个程序会被写入0X0开始的地址,开机后自动执行。
    那么我们需要解决以下几个问题:
   1.如何编译和调试主程序
   2.如何使中断跳到RAM中的中断服务程序执行
   3.如何把引导程序和主程序写入FLASH中.
以下我们来解决这几个问题:
  1 开始在仿真器中写代码和调试
  由于主程序会被拷贝到RAM中执行,则我们应该在编译时就把程序定位到RAM中。这里先要说说几个ADS的参数的意义,在ADS的ARM LINKER页有RO,RW两个参数,此外还有一个ZI没有在页中给出,RO是只读代码的起始地址,由这个地址开始存放编译出来的程序指令;RW是程序的读写段的开始,即你程序中的数据存放的开始地址,ZI紧跟在RW区后,ZI区存放的是需要在程序运行时初始化为0的数据。
了解这几个链接参数的意义后我们可以设置这几个参数了:对于我的44B0X板8M SDRAM在0XC00_0000.因此在开发时把ADS中的RO BASE的地址指定为0XC00_0000;置于RW,在程序完成前可以预先估计一下程序的体积有多大,需要用到的数据区有多大,避免数据区太小或代码区覆盖掉前面的数据区就是了,我用了0XC10_0000,1M的代码空间,其他作数据区。这样,我们编译出来的程序代码就是在0XC00_0000中,可以直接由仿真器写入RAM中运行仿真运行。此外,在linker-〉layout页有个object symbol和section的选项,要求你